我有一个AHAH请求的HTML,比如:
<table>
<tr>
<td>...</td>
<td><img src="..." onClick="get_next_ahah(sell####)" id="sell####"/></td>
</tr>
<tr>
<td>...</td>
<td><img src="..." onClick="get_next_ahah(sell####)" id="sell####"/></td>
</tr>
... and so on其中#### -来自数据库的数字ID。如何在高效的事件编写的jQuery函数中替换函数"get_next_ahah()“?我怎样才能知道我使用的是哪一个id呢?
发布于 2009-08-25 23:37:53
您可以使用一种相当模糊的CSS选择器来获取其ID包含文本"sell“的所有元素,然后使用该文本为它们分配事件:
$("[id^=sell]")或者,如果保证所有元素都是imgs,则可以使用这个更具体的选择器:
$("img[id^=sell]")这两个选择器都将返回ID中包含"sell“的元素数组,您可以对其调用click()。
要找出当前ID,只需将ID中的"sell“去掉,然后将其传递给get_next_ahah()函数,如下所示:
$("img[id^=sell]").click(function() {
get_next_ahah(this.id.replace('sell', '');
});发布于 2009-08-25 23:36:49
如果用下划线或以下划线将数字与前缀字符串分开,则从ID中提取数字会更容易,例如:
<img src="..." id="sell_1234"/>那么你只需要这样做:
$('table tr td img').click(function() {
var num = $(this).attr('id').split('_')[1];
get_next_ahah(num);
});如果您不能更改ID,那么只需使用正则表达式从字符串中提取数字,例如:
$('table tr td img').click(function() {
var num = $(this).attr('id').match(/\((\d+)\)/)[1];
get_next_ahah(num);
});https://stackoverflow.com/questions/1331547
复制相似问题