如何使用JS在提交表单后向元素添加CSS

我目前正在创建一个模因生成器应用程序,用户可以在其中提交图像以及顶部和底部文本。我想让它在提交表单后,将文本添加到图像上并使用 CSS 设置样式。我已经尝试向元素添加一个类并向其添加 css,但这不起作用。这是我的代码:


JS


let form = document.querySelector('#meme-form');

let img = document.querySelector('#img');

let topTxt = document.querySelector('#top-txt');

let bottomTxt = document.querySelector('#bottom-txt');


form.addEventListener('submit', function(e) {

    e.preventDefault();

    let memePic = document.createElement('img');

    //create the divs for the memes

    let newDiv = document.createElement('div');

    form.appendChild(newDiv);

    topTxt.classList.add('top')

    bottomTxt.classList.add('bottom')

    memePic.src = img.value;

    newDiv.append(memePic, topTxt.value, bottomTxt.value);


    //set the textbox inputs equal to nothing

    img.value = '';

    topTxt.value = '';

    bottomTxt.value= '';   


})

CSS


div {  

    width: 30%;

    height: 300px;

    margin: 10px;

    display: inline-block;

}

img {

    width: 100%;

    height: 100%;

    margin: 10px;

    display: inline-block;

}

.top{

    color: blue;

}

#bottom-txt {

    color: red;

}

HTML


<body>

    <form action="" id="meme-form">

        <label for="image">img url here</label>

        <input id="img" type="url"><br>

        <label for="top-text">top text here</label>

        <input id="top-txt" type="text"><br>

        <label for="bottom-text">bottom text here</label>

        <input id="bottom-txt" type="text"><br>

        <input type="submit"><br>

    </form>

    <script src="meme.js"></script>

</body>

</html>


慕哥9229398
浏览 144回答 1
1回答

汪汪一只猫

您只需要修复一些元素在提交后的logic状态。appendedform为此,您需要在您将持有您div之后,然后命令您进行展示。我还在每个显示之间添加了一条线。formresultselementhrseparatorresults你可以style按照你希望的方式来设置你的元素——我已经添加了一些基本的CSS来显示一些样式和一个img仅用于演示目的的实际 url。现场工作演示:let form = document.querySelector('#meme-form');let img = document.querySelector('#img');let topTxt = document.querySelector('#top-txt');let bottomTxt = document.querySelector('#bottom-txt');let results = document.querySelector('.meme-results');form.addEventListener('submit', function(e) {&nbsp; e.preventDefault();&nbsp; let memePic = document.createElement('img');&nbsp; var hrLine = document.createElement('hr');&nbsp; //create the divs for the memes&nbsp; let newDiv = document.createElement('div');&nbsp; let topText = document.createElement('span');&nbsp; let bttomText = document.createElement('span');&nbsp; //Top text&nbsp; topText.classList.add('top')&nbsp; topText.textContent = topTxt.value&nbsp; //Img&nbsp; memePic.src = img.value;&nbsp; results.appendChild(topText);&nbsp; results.append(memePic);&nbsp; //bottom text&nbsp; bttomText.classList.add('bottom')&nbsp; bttomText.textContent = bottomTxt.value&nbsp; results.append(bttomText);&nbsp; results.append(hrLine);&nbsp; //set the textbox inputs equal to nothing&nbsp; //img.value = '';&nbsp; topTxt.value = '';&nbsp; bottomTxt.value = '';}).meme-results {&nbsp; width: 30%;&nbsp; height: 300px;&nbsp; margin: 10px;&nbsp; display: block;}img {&nbsp; width: 100%;&nbsp; height: 100%;&nbsp; display: block;}.top, #top-txt {&nbsp; color: blue;}.bottom, #bottom-txt&nbsp; {&nbsp; color: red;}<html><body>&nbsp; <form action="" id="meme-form">&nbsp; &nbsp; <label for="image">img url here</label>&nbsp; &nbsp; <input id="img" type="url" value="https://via.placeholder.com/150"><br>&nbsp; &nbsp; <label for="top-text">top text here</label>&nbsp; &nbsp; <input id="top-txt" type="text"><br>&nbsp; &nbsp; <label for="bottom-text">bottom text here</label>&nbsp; &nbsp; <input id="bottom-txt" type="text"><br>&nbsp; &nbsp; <input type="submit"><br>&nbsp; </form>&nbsp; <div class="meme-results"></div>&nbsp; <script src="meme.js"></script></body></html>
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript