Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
173 views
in Technique[技术] by (71.8m points)

javascript - How to add line breaker in array and make it right aligned

I am noob in java script .Just made a small project that generates quotes. But i want that that the name of the person who gave the quote should be in next line and right aligned but cant figure out how to do that in an array line breaker tag does not work.

HTML

<body>
    <div class="container">
        <div class="container-fluid">
            <button type="button" class="btn btn-success">Generate Quote</button>
            <div class="row">

                <div class="col-lg-12">

                </div>
            </div>
        </div>
    </div>

    <script src="index.js" async defer>
    </script>
</body>

JS

let quotes=["I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.","You only live once, but if you do it right, once is enough."];
let btn=document.querySelector(".btn");
let adding=document.querySelector(".col-lg-12");
btn.addEventListener("click",function(){
    let myQuote=quotes[getRandomNumber()];
    adding.textContent=myQuote;
 
  })

console.log(getRandomNumber());

function getRandomNumber(){
return (Math.floor(Math.random()*quotes.length))};

Thank You For your precious time!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Adding a tag can make a new line. Making the text aligned at the right need CSS.

let quotes=["I'm selfish, impatient and a little insecure. I make mistakes, I am out of control and at times hard to handle. But if you can't handle me at my worst, then you sure as hell don't deserve me at my best.","You only live once, but if you do it right, once is enough."];

let reference = '-Unknown'
let aTag = document.createElement('p');
aTag.style.textAlign = 'right';

let btn=document.querySelector(".btn");
let adding=document.querySelector(".col-lg-12");
btn.addEventListener("click",function(){
    let myQuote=quotes[getRandomNumber()];
    adding.textContent=myQuote;
    aTag.textContent = reference;
    adding.appendChild(aTag);
  })

console.log(getRandomNumber());

function getRandomNumber(){
return (Math.floor(Math.random()*quotes.length))};
<body>
    <div class="container">
        <div class="container-fluid">
            <button type="button" class="btn btn-success">Generate Quote</button>
            <div class="row">

                <div class="col-lg-12">

                </div>
            </div>
        </div>
    </div>

    <script src="index.js" async defer>
    </script>
</body>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...