I've got the following code inside of a <script>
tag:
$(document).ready(function(){
$("#toolbarReset").click(function() {
alert("I've been clicked! Oh, what a world!");
}开发者_运维百科);
... other, unrelated functions, which all seem to work fine
});
... and some html below:
<span id="toolbarReset" style="line-height: 18px; width: 50px; height: 21px; padding: 0pt 7px 0pt 3px;">
Reset
</span>
... but when I click on the words "Reset", nothing happens.
Tried changing it from using "id" to "class", and updating the jQuery code appropriately ( "#toolbarReset" became "toobarReset" ). Tried putting some code inside the .click event to output to the Firebug console, and verified that "toolbarReset" is a unique id within the DOM.
Seems to work for me, what browser are you using?
http://jsfiddle.net/YLCff/
maybe you have more than one #toolbarReset, if this is the case it will not work until you remove other ids.
Turned out to be a timing issue. My jQuery code was running before the element was loaded on the page, due to where I placed them in relation to one another - hence the jQuery was trying to bind to an ID that didn't yet exist.
I checked the code in my browser it worked . Anyway try using .on instead of .click.
i.e
$("#toolbarReset").on('click',function() {
// watever code
}
精彩评论