我有以下jQuery代码,它根据id属性检测单击了哪个<img>
标记。然后,如果单击“删除”按钮,则将删除/删除所选的<img>
。
$(document).ready(function () {
$("img").click(function () {
var frame = $(this).attr("id"); //can it be done this way?
$("button").click(function () {
$("img").remove(frame); //can I pass the event in as a parameter?
$("button").unbind();
$(this).unbind();
});
});
});
我是jQuery的新手,因此在语法上很难。感谢所有伟大的帮助。
答案 0 :(得分:0)
这一行:
$("img").remove(frame);
应该是:
$("#"+frame).remove();
前提是,您为img
提供了一个唯一ID。
完整代码&amp;演示: http://jsfiddle.net/Gusn7/
答案 1 :(得分:0)
你可以尝试这样..
<强> HTML 强>
<img id="someId" src="http://placehold.it/250x250">
<button target-id="someId">Delete</button>
<强>的jQuery 强>
$(document).ready(function () {
$("button").click(function () {
var frame = $(this).attr("target-id");
$("#" + frame).remove();
$("button").unbind();
$(this).unbind();
});
});
演示: http://jsfiddle.net/Gusn7/1/
target-id
是自定义属性。它应该是id
img
如果button
始终位于image
旁边,则可以使用此
$(document).ready(function () {
$("button").click(function () {
$(this).prev("img").remove();
$(this).unbind();
});
});
演示: http://jsfiddle.net/Gusn7/3/
您的代码错误
$("img").click(function () {
$("button").click(function () {
应该是任何一项活动...... img
点击或button
点击
答案 2 :(得分:0)
$(document).ready(function () {
$("button").click(function () {
("#imgIDName").remove();
});
});
答案 3 :(得分:0)
尝试
$(document).ready(function () {
var $imgs = $('img');
$("img").click(function () {
$imgs.removeClass('selected');
$(this).addClass('selected');
})
$("button").click(function () {
$("img.selected").remove(); //can I pass the event in as a parameter?
});
});
演示:Fiddle