简体   繁体   English

为什么更改jQuery UI Button的图标会破坏我在Internet Explorer中的链接?

[英]Why does changing the icon of a jQuery UI Button break my link in Internet Explorer?

I am trying to change the icon of a button as soon as it was used. 我试图在使用按钮时立即更改按钮的图标。 The code is pretty simple: 代码非常简单:

<a id='just-a-link' target="_blank" href="http://google.com">Google</a>

jQuery('#just-a-link').button().bind('click', function() {

    var icons = jQuery(this).button("option", "icons");
    icons.primary = 'ui-icon-plus';

    jQuery(this).button("option", "icons", icons);

});

However Internet Explorer the link does not get fired. 但Internet Explorer的链接不会被触发。 So clicking on the link does change the icon but does not open the page . 因此, 单击链接会更改图标,但不会打开页面

What do you suggest to solve this? 您有什么建议来解决这个问题?

http://jsfiddle.net/YnwnU/ http://jsfiddle.net/YnwnU/

In the fiddle you can see that jQuery(this).button("option", "icons", icons); 在小提琴中你可以看到jQuery(this).button("option", "icons", icons); causes the bug as the second button does open the link. 当第二个按钮打开链接时导致错误。

Alright well after looking at it some more, it looks like since you are calling .button() inside of an already established .button() call, it happens to be killing -href- or whatever eventHandlers were already set on it. 看它多一些后好了好了,它看起来像,因为你在呼唤.button()已建立的内部.button()调用,它正好可以查杀-href-或任何事件处理器已经设置就可以了。 (In this case opening a new window & link. (在这种情况下打开一个新窗口和链接。

jQuery('#just-a-link').button().bind('click', function() {
    var icons = jQuery(this).button("option", "icons");
    icons.primary = 'ui-icon-plus';

    // Adding this makes it work in IE for at least the code you currently have
    window.open($(this).attr('href'));
});

It looks more to me like you are trying to simply add an icon to all external links, and open them in a new window. 对我来说,更像是在尝试简单地向所有外部链接添加图标,并在新窗口中打开它们。 Why not just do this instead? 为什么不这样做呢?

var icons = jQuery(this).button("option", "icons");
    icons.primary = 'ui-icon-plus';

jQuery('a[target="_blank"]').button().button("option", "icons", icons);

No need for binding a click... just setting all anchors with target="_blank" to button & giving them the Icon. 无需绑定点击...只需将所有锚点与target="_blank"为按钮并为其提供图标。 This also makes it work automatically in IE and other browsers. 这也使它在IE和其他浏览器中自动运行。 Give it a go. 搏一搏。

updated jsFiddle Demo 更新了jsFiddle演示

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

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