On adding a class, .click does not trigger - Using jQuery
I added a span with a class, but .click does not trigger.
Adding a span class code
var TA 开发者_Python百科= '<span class="TS" id="'+$('#TT').attr('value')+'" style=" color:#fff; margin-left:5px; font-family:arial; font-size:12px">* '+$('#TT').attr('value')+'</span>';
$('#QLT').append(TA);
This adds a span .TS to a div .TX. I want to trigger .TS using .click, the code
$('.TS').click(function() {
alert("ok");
});
But this does not trigger. What is wrong, Appreciate all assistance.
Thanks Jean
You probably defined the click handler before you did $('#QLT').append(TA)
. Thus the click handler didn't know about the new element with class .TS
when it was bound.
You can manually re-bind it, or use jQuery's cool live function, which will automatically bind when a new element with class TS
is inserted into the DOM:
$('.TS').live('click', function() {
alert("ok");
});
It looks like your code is working fine.
Maybe when you're clicking, you're not actually clicking on the text in the span. I think that messes things up sometimes, with click handlers.
精彩评论