admin管理员组文章数量:1334704
This function uses jQuery to modify the contents of a DOM element. What am I doing wrong?
function updateScore(score) {
console.log("Test score is: " + score);
$("#testScore").innerHTML = 'Current score is:' + score;
}
updateScore(1000);
<script src=".7.1/jquery.min.js"></script>
<p id="testScore">
This function uses jQuery to modify the contents of a DOM element. What am I doing wrong?
function updateScore(score) {
console.log("Test score is: " + score);
$("#testScore").innerHTML = 'Current score is:' + score;
}
updateScore(1000);
<script src="https://cdnjs.cloudflare./ajax/libs/jquery/3.7.1/jquery.min.js"></script>
<p id="testScore">
The log message shows up, but nothing else does. I have a <p>
with the id testScore
, but it doesn't change when I run the function. Why?
3 Answers
Reset to default 10Try .html()
on a jQuery object. innerHTML
is for DOM-Elements.
$("#testScore").html('Current score is: '+ bucket.score);
If, for some reason, you really want to use innerHTML
, you can convert the jQuery Object back to its DOM variant, for example using [0] or .get(0). Call like this, then:
$("#testScore")[0].innerHTML ='Current score is: '+ bucket.score
But I don't see why you would want to do that - since you're already writing in jQuery, there's no need to fallback to DOM methods that have a perfectly fine jQuery equivalent.
function updateScore() {
alert("Test score is: " + bucket.score);
$("#testScore").text('Current score is: ' + bucket.score);
}
try
$("#testScore").innerHTML('Current score is: + bucket.score');
The jQuery objects do not support assignment to the attributes. (Its a Javascript limitation) You have to call the function with a parameter to set something.
本文标签: javascriptWhy doesn39t innerHTML work on jQuery collectionsStack Overflow
版权声明:本文标题:javascript - Why doesn't innerHTML work on jQuery collections? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742366055a2461276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论