使用带有innerHTML函数的javascript添加“div”和“input”

时间:2014-02-12 10:47:44

标签: javascript

我已经写了这个javascript代码

document.getElementById('placeForTitle').innerHTML=
"<div class='postAdLabelnTextboxSelTitle'>
       <div class='post-ad-label'>Select Title</div>
       <div class='selectTitleContain'>
           <input name='selctedTitle' type='radio' id='tit1_1' value='"+title1_1+"' required />
           <label for='tit1_1'>"+title1_1+"</label>
           <br />
           <input name='selctedTitle' type='radio' id='tit1_2' value='"+title1_2+"' required />
           <label for='tit1_2'>"+title1_2+"</label><
       /div>
</div>";

但它不起作用,由于这个其他的JavaScript功能也无法正常工作。我尝试了不同的组合但没有工作。

4 个答案:

答案 0 :(得分:1)

试试这个:

document.getElementById('placeForTitle').innerHTML=
"<div class='postAdLabelnTextboxSelTitle'>\
       <div class='post-ad-label'>Select Title</div>\
       <div class='selectTitleContain'>\
           <input name='selctedTitle' type='radio' id='tit1_1' value='"+title1_1+"' required />\
           <label for='tit1_1'>"+title1_1+"</label>\
           <br />\
           <input name='selctedTitle' type='radio' id='tit1_2' value='"+title1_2+"' required />\
           <label for='tit1_2'>"+title1_2+"</label>\
       </div>\
</div>";

document.getElementById('placeForTitle').innerHTML=
"<div class='postAdLabelnTextboxSelTitle'>" +
       "<div class='post-ad-label'>Select Title</div>" +
       "<div class='selectTitleContain'>" +
           "<input name='selctedTitle' type='radio' id='tit1_1' value='"+title1_1+"' required />" +
           "<label for='tit1_1'>"+title1_1+"</label>" +
           "<br />" +
           "<input name='selctedTitle' type='radio' id='tit1_2' value='"+title1_2+"' required />" +
           "<label for='tit1_2'>"+title1_2+"</label>" +
       "</div>" +
"</div>";

答案 1 :(得分:0)

您不能在JavaScript字符串文字的中间添加新行。

答案 2 :(得分:0)

document.getElementById('placeForTitle').innerHTML="<div class='postAdLabelnTextboxSelTitle'>"
       +"<div class='post-ad-label'>Select Title</div>"
       +"<div class='selectTitleContain'>"
           +"<input name='selctedTitle' type='radio' id='tit1_1' value='"+title1_1+"' required />"
           +"<label for='tit1_1'>"+title1_1+"</label>"
           +"<br />"
           +"<input name='selctedTitle' type='radio' id='tit1_2' value='"+title1_2+"' required />"
           +"<label for='tit1_2'>"+title1_2+"</label></div>"
+"</div>";

答案 3 :(得分:0)

试试这个

document.getElementById('placeForTitle').innerHTML=
"<div class='postAdLabelnTextboxSelTitle'><div class='post-ad-label'>Select Title</div><div class='selectTitleContain'><input name='selctedTitle' type='radio' id='tit1_1' value='"+title1_1+"' required /><label for='tit1_1'>"+title1_1+"</label><br /><input name='selctedTitle' type='radio' id='tit1_2' value='"+title1_2+"' required /><label for='tit1_2'>"+title1_2+"</label></div></div>";

你不应该破坏innerHTML内容。