无法使用 JavaScript 更改标签文本
Unable to change label text using JavaScript
更新:
我再次检查了我的代码。它的问题实际上是函数的位置(我没有 post 它在这里)并且我已经修复了它!
这只是我的代码的简短摘要。但它不会 运行 即使我将 js 放在 我的标签之后。
< script type = "text/javascript" >
document.getElementById('lblSport').innerHTML = 'Hello'; < /script>
<label class="lblSport" id="lblSport">asas</label>
您必须等待页面加载,删除脚本标签上的 space 并且 class 和 ID
的名称不同
window.onload = function(){
document.getElementById('lblSport').innerHTML = 'Hello';
}
<label class="lblSports" id="lblSport">asas</label>
删除脚本标签前和脚本标签末尾的space即可。
<script> .... </script>
您的开始和结束脚本 html 标签包含不需要的 space。
它应该是这样的:
<script></script>
同样,根据 HTML5:
The id attribute specifies its element's unique identifier (ID). The
value must be unique amongst all the IDs in the element's home subtree
and must contain at least one character. The value must not contain
any space characters.
http://www.w3.org/TR/html5/dom.html#the-id-attribute
虽然您可以为 class 和 ID 使用相同的名称,但最佳做法可能是避免使用相同的名称,主要是为了便于阅读。
你的代码没问题,检查这个demo
window.onload = function(){
document.getElementById('lblSport').innerHTML = 'Hello';
};
只需确保在更改其 innerHTML
之前将元素加载到 DOM
看到这个fiddle:https://jsfiddle.net/wk63pov0/
<label id="demo" >(innerHTML).</label>
Js:
document.getElementById("demo").innerHTML = " changed!";
请 post 您的完整 html。
标签在您的案例中未定义,因为您的代码在实际标签出现在页面上之前被调用。尝试将您的代码添加到 "document ready" 函数中以使其起作用。文档就绪函数将在文档准备就绪时触发(一旦文档加载了所有元素)。
<script type = "text/javascript">
$(document).ready(function () {
document.getElementById('lblSport').innerHTML = 'Hello';
});
</script>
<label class="lblSport" id="lblSport">asas</label>
更新: 我再次检查了我的代码。它的问题实际上是函数的位置(我没有 post 它在这里)并且我已经修复了它!
这只是我的代码的简短摘要。但它不会 运行 即使我将 js 放在 我的标签之后。
< script type = "text/javascript" >
document.getElementById('lblSport').innerHTML = 'Hello'; < /script>
<label class="lblSport" id="lblSport">asas</label>
您必须等待页面加载,删除脚本标签上的 space 并且 class 和 ID
的名称不同window.onload = function(){
document.getElementById('lblSport').innerHTML = 'Hello';
}
<label class="lblSports" id="lblSport">asas</label>
删除脚本标签前和脚本标签末尾的space即可。
<script> .... </script>
您的开始和结束脚本 html 标签包含不需要的 space。 它应该是这样的:
<script></script>
同样,根据 HTML5:
The id attribute specifies its element's unique identifier (ID). The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.
http://www.w3.org/TR/html5/dom.html#the-id-attribute
虽然您可以为 class 和 ID 使用相同的名称,但最佳做法可能是避免使用相同的名称,主要是为了便于阅读。
你的代码没问题,检查这个demo
window.onload = function(){
document.getElementById('lblSport').innerHTML = 'Hello';
};
只需确保在更改其 innerHTML
之前将元素加载到 DOM看到这个fiddle:https://jsfiddle.net/wk63pov0/
<label id="demo" >(innerHTML).</label>
Js:
document.getElementById("demo").innerHTML = " changed!";
请 post 您的完整 html。
标签在您的案例中未定义,因为您的代码在实际标签出现在页面上之前被调用。尝试将您的代码添加到 "document ready" 函数中以使其起作用。文档就绪函数将在文档准备就绪时触发(一旦文档加载了所有元素)。
<script type = "text/javascript">
$(document).ready(function () {
document.getElementById('lblSport').innerHTML = 'Hello';
});
</script>
<label class="lblSport" id="lblSport">asas</label>