I have an <a>
tag coming back from a php file in a json format, it is as follows:
$subarray[] = '<a href="#" onclick="profile(\'' . $row['id'] . '\')" class="buttons">...</a>';
This one works fine. However, reading some material on unobtrusive JavaScript, I'd like to remove that onclick="profile(\'' . $row['id'] . '\')"
and instead add an id
to the tag, and then listen for the time the id is clicked. Hence, the code will be something like the following:
$subarray[] = '<a href="#" class="buttons" id="some">...</a>';
但是,当我尝试监听事件的发生时,请使用以下代码:
$("#some").click(function() {
alert('hi');
});
什么都没发生。如果您能告诉我如何解决问题,并告诉我最佳做法是什么,我将不胜感激。
试试这个,当我遇到同样的问题时为我工作,当我没有加载对象时就会发生