字体真棒作为一个按钮,当我点击字体时它不能正常工作
Font awesome as a button, when i click on the font it does not work ptoperly
当我点击 'x' 时它不起作用,只有当我点击外面时它才起作用(id #close 有一个 3px 的填充)。我尝试将 #id 分配给 i 标签,它甚至没有触发事件。我该怎么做才能在触发点击事件时关闭 #id 内的任何内容。
html
<span id="close"><i class="fa fa-times-circle"></i></span>
js
$('#close').click(function(event){
if(event.target.id=='close'){
$(this).parent().parent().remove();
$(this).remove();
}
});
为什么使用这个条件?
if(event.target.id=='close')
我创建了这个 JSFiddle,它可以工作:
JSFiddle
您的代码没有按预期工作,因为 event.target.id
并非每次都 close
。这里有一个JSFiddle来说明问题。
您要做的只是处理 #close
上的 click
事件。请注意,$(this)
始终 当前选择器:
$("#close").click(function(event) {
var $topParent = $(this).parent().parent();
$(this).remove();
$topParent.remove();
});
当我点击 'x' 时它不起作用,只有当我点击外面时它才起作用(id #close 有一个 3px 的填充)。我尝试将 #id 分配给 i 标签,它甚至没有触发事件。我该怎么做才能在触发点击事件时关闭 #id 内的任何内容。
html
<span id="close"><i class="fa fa-times-circle"></i></span>
js
$('#close').click(function(event){
if(event.target.id=='close'){
$(this).parent().parent().remove();
$(this).remove();
}
});
为什么使用这个条件?
if(event.target.id=='close')
我创建了这个 JSFiddle,它可以工作: JSFiddle
您的代码没有按预期工作,因为 event.target.id
并非每次都 close
。这里有一个JSFiddle来说明问题。
您要做的只是处理 #close
上的 click
事件。请注意,$(this)
始终 当前选择器:
$("#close").click(function(event) {
var $topParent = $(this).parent().parent();
$(this).remove();
$topParent.remove();
});