简体   繁体   English

Bootstrap data-toggle =“按钮”无法使用

[英]Bootstrap data-toggle=“button” not working with <span>

I'm using the 'data-toggle'-option from Bootstrap for a button. 我正在使用Bootstrap中的'data-toggle'-选项作为按钮。 In the button I have a with glypicons and sometext. 在按钮中,我有一个glypicons和sometext。 However, when you click on the text it works, but when you click on the glyphicon it doesn't trigger the data-toggle. 但是,当您单击文本时它起作用,但是当您单击glyphicon时,它不会触发数据切换。 Since the data-toggle does not work in JSFiddle I cannot post a link to it. 由于数据切换在JSFiddle中不起作用,因此无法发布指向它的链接。

HTML: HTML:

<button type="button" id="showHide" class="btn btn-default" data-toggle="button"><span class="glyphicon glyphicon-eye-open"></span> show</button>

JavaScript: JavaScript的:

document.getElementById("showHide").onclick = function () {
    if ($("#showHide").text() == " show") {
        document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-close\"></span> hide";
        document.getElementById("showHide").blur();
    } else {
        document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-open\"></span> show";
        document.getElementById("showHide").blur();
    }
}

I've looked online but haven't found a solution or someone with the same problem. 我在网上看过,但没有找到解决方案或遇到相同问题的人。

It appears the span is being replaced before the toggle event is propogated. 在切换事件传播之前,似乎跨度已被替换。 You can use the button method to activate the toggle rather than the data attribute. 您可以使用button方法而不是数据属性来激活切换。

<button type="button" id="showHide" class="btn btn-default"><span class="glyphicon glyphicon-eye-open"></span> show</button>

document.getElementById("showHide").onclick = function () {
    if ($("#showHide").text() == " show") {
        document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-close\"></span> hide";
        jQuery("#showHide").button('toggle');        
        document.getElementById("showHide").blur();
    } else {
        document.getElementById("showHide").innerHTML = "<span class=\"glyphicon glyphicon-eye-open\"></span> show";
        jQuery("#showHide").button('toggle');        
        document.getElementById("showHide").blur();
    }
}

View Fiddle 查看小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM