【问题标题】:onclick span not working inside a button [duplicate]onclick span 在按钮内不起作用[重复]
【发布时间】:2015-06-30 21:40:35
【问题描述】:

我想要的是,当单击按钮内的 (X) 时,它应该触发注册的相应回调。但我不知道为什么它失败了。以下是我尝试过的:

HTML

<div id="cover"></div>
<template id='filtertemp'>
                <button id="filtype" class='filbut' data-filter=""></button>
</template>
<button id='create' onclick="filter();">Click</button>

JQUERY

function filter(){
var content= $('#filtertemp')[0].content;
if(content){
    var filType= content.querySelector('#filtype');
    $(filType).text("type : sometype");
    var clone= document.importNode(content,true);
    $('#cover').append($(clone));
    $('#cover > .filbut:last-child').append('<span class="glyphicon glyphicon glyphicon-remove"></span>');
}
$('.glyphicon').on('click', function(){
    alert('Remove');
});
}

这里是小提琴链接:http://jsfiddle.net/wtce6dje/

我什至尝试在下面注册监听器,但这似乎也起作用了:

$('body').on('click','.glyphicon', function(){
    alert('Remove');
});

【问题讨论】:

  • 在共享小提琴中工作正常。
  • 你需要这种东西吗jsfiddle.net/wtce6dje/1
  • 但 id 应该是唯一的
  • @MilindAnantwar 对我不起作用..我正在使用 mozilla 浏览器
  • @gmo 如果它不适用于 mozilla,有什么解决方法?

标签: jquery html twitter-bootstrap


【解决方案1】:
<button id='create' Onclick="return filter();">Click</button>

【讨论】:

    【解决方案2】:

    在 Chrome 中在按钮中设置点击事件对我有效,但在我测试过的其他浏览器中无效。

    当它被一个 div 换出时它可以工作:

    <template id='filtertemp'>
        <div id="filtype" class='filbut' data-filter=""></div>
    </template>
    

    【讨论】:

      猜你喜欢
      • 2017-02-06
      • 2013-03-09
      • 1970-01-01
      • 2018-05-10
      • 1970-01-01
      • 2014-06-22
      • 2014-10-19
      • 1970-01-01
      • 2012-03-26
      相关资源
      最近更新 更多