我有一个img和一个按钮,我想使用jQuery绑定来响应onclick事件。绑定在输入按钮上有效,但在img上无效。下面是我的JavaScript代码:
function drawDetails (event) {
var id=0;
if ((typeof $(this).attr('id')) !== 'undefined') {
id = parseInt($(this).attr('id').replace(/[^0-9]+/g,''));
} else if((typeof e.id)!=='undefined') {
id = e.id;
}
alert("drawDetails " + id);
}
$("input[id^=camera-]").bind('click', drawDetails);
$("input[id^=details-]").bind('click', drawDetails);HTML:
<img id="camera-10" src="camera.gif"/>
<input id="details-10" type="button" value="Details 10">
<img id="camera-11" src="camera.gif"/>
<input id="details-11" type="button" value="Details 11">我已经准备了一份JSFiddle。如何使事件在图像被单击时也被激发?
发布于 2011-11-11 01:34:02
使用img而不是input
$("img[id^=camera-]").bind('click', drawDetails);JSFiddle
发布于 2011-11-11 01:35:15
尝尝这个
$("img[id^=camera-]").bind('click', drawDetails);
$("input[id^=details-]").bind('click', drawDetails);示例http://jsfiddle.net/eJ3rn/4/
https://stackoverflow.com/questions/8083774
复制相似问题