admin管理员组文章数量:1287270
This is probably a stupid mistake, but i can't seem to get this to work. I'm trying to change the innerhtml of all the H2 elements withing the div whose id=variable id.
var numberOfQuestions = $('.question').length;
var id = "question"+(numberOfQuestions);
clone.id=id;
document.documentElement.getElementById(id).getElementsByTagName( "h2" ).innerhtml= "Question"+(numberOfQuestions);
I think I'm doing something wrong here: document.documentElement.getElementById(id).getElementsByTagName( "h2" ).innerhtml= "Question"+(numberOfQuestions);
The nrtire script:
<script type="text/javascript">
function copyAppendRow() {
var question = document.getElementById("question");
var clone=question.cloneNode(true);
var numberOfQuestions = $('.question').length;
var id = "question"+(numberOfQuestions);
clone.id=id;
var questiondiv = document.getElementById(id);
var h2s = questiondiv.getElementsByTagName("h2");
for(var h = 0; h < h2s.length; h++ ) {
h2s[h].innerHTML = "Question"+(numberOfQuestions); }
if($('#questionsuccess').css('display') == 'none'){
$('#questionsuccess').fadeIn('fast');
$('#questionsuccess').fadeOut(4000);
}
}
</script>
This is probably a stupid mistake, but i can't seem to get this to work. I'm trying to change the innerhtml of all the H2 elements withing the div whose id=variable id.
var numberOfQuestions = $('.question').length;
var id = "question"+(numberOfQuestions);
clone.id=id;
document.documentElement.getElementById(id).getElementsByTagName( "h2" ).innerhtml= "Question"+(numberOfQuestions);
I think I'm doing something wrong here: document.documentElement.getElementById(id).getElementsByTagName( "h2" ).innerhtml= "Question"+(numberOfQuestions);
The nrtire script:
<script type="text/javascript">
function copyAppendRow() {
var question = document.getElementById("question");
var clone=question.cloneNode(true);
var numberOfQuestions = $('.question').length;
var id = "question"+(numberOfQuestions);
clone.id=id;
var questiondiv = document.getElementById(id);
var h2s = questiondiv.getElementsByTagName("h2");
for(var h = 0; h < h2s.length; h++ ) {
h2s[h].innerHTML = "Question"+(numberOfQuestions); }
if($('#questionsuccess').css('display') == 'none'){
$('#questionsuccess').fadeIn('fast');
$('#questionsuccess').fadeOut(4000);
}
}
</script>
Share
Improve this question
edited Oct 7, 2012 at 6:13
Atom Vayalinkal
asked Oct 7, 2012 at 5:50
Atom VayalinkalAtom Vayalinkal
2,7028 gold badges30 silver badges37 bronze badges
2 Answers
Reset to default 7do you mean something like:
var divEle = document.getElementById("yourDivId");
var h2s = divEle.getElementsByTagName("h2");
for(var h = 0; h < h2s.length; h++ ) {
h2s[h].innerHTML = "Question"+(numberOfQuestions);
}
OR jQuery way:
$("#"+yourDivId + " > h2").html("Question"+(numberOfQuestions));
I see from your first line that you are already using jQuery, so make life easy for yourself and use it to do this task.
$('#' + id + ' h2').html( 'Question ' + numberOfQuestions );
The jQuery selectors work just like CSS selectors. So this line of code finds the element with your variable id
as its' id and gets all the h2 tags within that element. .html
is a jQuery method that sets the inner HTML of an element.
本文标签: javascriptChange innerhtml of all h2 elements within divStack Overflow
版权声明:本文标题:javascript - Change innerhtml of all h2 elements within div - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741241336a2364037.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论