简体   繁体   中英

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. The alert works, it retrieves #25. That's the id that I gave one of my labels..

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

What do I do?

That's how the label html control looks like when I view the source page:

<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?!?

UPDATE 2: This doesnt work:

document.getElementById($commentID).value="88888";

The label isnt changed.

You do not have to supply the # for getElementById . Just the ID. So write:

var $label=document.getElementById($commentID).value;

Another option is to get the element via the JQuery $ function. Then you have to supply the # :

var $label=$($theID).val()

document.getElementById("your_id_without_#_char") will return you HTML element.

$("#your_id_with_#_char") will return you jQuery object.

You shouldn't use # for document.getElementById() function - just elem ID.

Not add the "#" to your 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 # :

var $label = document.getElementById($commentID);

or use jQuery:

var $label = $($theID);

Note that label s don't have a value property. If you want to get the inner text, use innerHTML or .text() with jQuery.

Also, before HTML5, IDs where not allowed to start with digits.

If you want to use the # in front of your id, you can use the querySelector method, but is not compatible with older browsers.

Incompatible with: IE < 8, FFox < 3.5, Opera < 10, Safari < 3.2

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

$('#id');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM