[英]getElementById issue
I have got this button: 我有这个按钮:
<input type="button" value="Rep"
id="rep" name="rep" class="rep" style="width:50px"
onclick="triggerLabel(1);this.disabled=true;addRep(<?php echo $comment_id; ?>);"/>
<script type="text/javascript">
function triggerLabel($commentID)
{
var $theID="#"+$commentID;
var $label=document.getElementById($theID).value;
alert($label);
}
</script>
The alert isn't triggered. 警报未触发。 I want to obtain the commentID and convert it to a string.. And I want that string to represent my element ID.
我想获取commentID并将其转换为字符串。而且我希望该字符串代表我的元素ID。 The alert works, it retrieves #25.
警报起作用,它检索#25。 That's the id that I gave one of my labels..
那是我给我的一个标签提供的ID。
echo '<label id="'.$comment_id.'">'.$avatarRep.'</label>';
It should retrieve that label, but unfortunately it fails..I get this: 它应该检索该标签,但不幸的是它失败了。
Error: document.getElementById($theID) is null Source File: http://localhost/PoliticalForum/Thread/thread.php?threadID=15&page=1 Line: 246
错误:document.getElementById($ theID)为空源文件: http://localhost/PoliticalForum/Thread/thread.php?threadID = 15&page = 1行:246
What do I do? 我该怎么办?
That's how the label html control looks like when I view the source page: 这就是我查看源页面时html标签控件的外观:
<label id="25">6</label>
UPDATE: I updated my answer. 更新:我更新了答案。 Take a look:
看一看:
var $label=document.getElementById($commentID);
$label.value=9999;
The 9999 isnt inserted into the table..why?!? 桌子上没有插入9999。为什么?!?
UPDATE 2: This doesnt work: 更新2:这不起作用:
document.getElementById($commentID).value="88888";
The label isnt changed. 标签未更改。
You do not have to supply the #
for getElementById
. 您不必为
getElementById
提供#
。 Just the ID. 只是ID。 So write:
所以写:
var $label=document.getElementById($commentID).value;
Another option is to get the element via the JQuery $
function. 另一种选择是通过JQuery
$
函数获取元素。 Then you have to supply the #
: 然后,您必须提供
#
:
var $label=$($theID).val()
document.getElementById("your_id_without_#_char")
will return you HTML element. document.getElementById("your_id_without_#_char")
将返回HTML元素。
$("#your_id_with_#_char")
will return you jQuery object. $("#your_id_with_#_char")
将返回jQuery对象。
You shouldn't use #
for document.getElementById()
function - just elem ID. 您不应该将
#
用于document.getElementById()
函数-仅使用elem ID。
Not add the "#" to your CommentID. 不要在您的CommentID中添加“#”。 write:
写:
function triggerLabel($commentID)
{
var $theID=$commentID;
var $label=document.getElementById($theID).value;
alert($label);
}
document.getElementById()
不是jQuery,它是DOM函数,因此您无需在id的前面放置#
Either use getElementById
properly, ie remove the #
: 正确使用
getElementById
,即删除#
:
var $label = document.getElementById($commentID);
or use jQuery: 或使用jQuery:
var $label = $($theID);
Note that label
s don't have a value
property. 请注意,
label
s没有value
属性。 If you want to get the inner text, use innerHTML
or .text()
with jQuery. 如果要获取内部文本,请对jQuery使用
innerHTML
或.text()
。
Also, before HTML5, IDs where not allowed to start with digits. 另外,在HTML5之前,不允许以数字开头的ID。
If you want to use the # in front of your id, you can use the querySelector
method, but is not compatible with older browsers. 如果要在ID前面使用# ,则可以使用
querySelector
方法,但与旧版浏览器不兼容。
Incompatible with: IE < 8, FFox < 3.5, Opera < 10, Safari < 3.2
不兼容:IE <8,FFox <3.5,Opera <10,Safari <3.2
https://developer.mozilla.org/En/DOM/Document.querySelector https://developer.mozilla.org/En/DOM/Document.querySelector
document.querySelector('#id');
You should not use the hash in your case 您不应该在情况下使用哈希
document.getElementById('#id');
in jquery you can use the hash 在jQuery中,您可以使用哈希
$('#id');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.