简体   繁体   English

jQuery 单击字体真棒图标在所有浏览器中未一致注册

[英]jQuery click on font-awesome icon not registering consistently in all browsers

I am trying out the code in this fiddle .我正在尝试这个fiddle 中的代码。 It just contains a font-awesome icon, clicking which is supposed to show a simple alert.它只包含一个字体很棒的图标,点击它应该会显示一个简单的警报。 It works fine on Vivaldi, Safari and Chrome.它在 Vivaldi、Safari 和 Chrome 上运行良好。 However, on Edge, it is extremely flaky, often not registering the click and not alerting, although sometimes it does.但是,在 Edge 上,它非常不稳定,通常不会记录点击,也不会发出警报,尽管有时会。

Is there any Edge specific problem regarding font-awesome icons?关于字体真棒图标是否有任何 Edge 特定问题? Is there any known fix for this, if so?如果是这样,是否有任何已知的修复方法?

The code:代码:

$(function() {
    $(".scoreCardExpand").on("click", function() {  
    alert(5);
  });
});

Read: https://www.chromestatus.com/feature/5148698084376576阅读: https : //www.chromestatus.com/feature/5148698084376576
Don't use alert() use rather console.log()不要使用alert()而是使用console.log()

Tl;Dr : Tl;博士
Seems Edge conforms with the Remove alert() feature receipt - when an alert if started from an origin different that the page embedding it.似乎 Edge 符合Remove alert()功能接收 - 当警报从与嵌入它的页面不同的源开始时。 jsfiddle.net uses an iframe (to show the code panes) with a slightly different origin: fiddle.jshell.net . jsfiddle.net使用iframe (用于显示代码窗格),其来源略有不同: fiddle.jshell.net
Why is not the same for Chrome (both being on Chromium) - I'm not sure at this point.为什么 Chrome 不一样(都在 Chromium 上) - 我现在不确定。

Another issue might be (inspect element to see de-facto) - that fas icons JS library for some stupid nonsensical reason removes (comments out <!-- --> ) the original i tag and replaces it with a brand new <svg> but copyes the original className (ie: .scoreCardExpand ):另一个问题可能是(检查元素以查看实际情况)- fas图标 JS 库出于一些愚蠢的荒谬原因删除(注释掉<!-- --> )原始i标记并将其替换为全新的<svg>但复制原始类名(即: .scoreCardExpand ):

<svg class="svg-inline--fa fa-chevron-circle-down fa-w-16 scoreCardExpand" aria-hidden="true" data-fa-processed="" data-prefix="fas" data-icon="chevron-circle-down" role="img" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 512 512"><path fill="currentColor" d="M504 256c0 137-111 248-248 248S8 393 8 256 119 8 256 8s248 111 248 248zM273 369.9l135.5-135.5c9.4-9.4 9.4-24.6 0-33.9l-17-17c-9.4-9.4-24.6-9.4-33.9 0L256 285.1 154.4 183.5c-9.4-9.4-24.6-9.4-33.9 0l-17 17c-9.4 9.4-9.4 24.6 0 33.9L239 369.9c9.4 9.4 24.6 9.4 34 0z"></path></svg>
<!-- <i class="fas fa-chevron-circle-down scoreCardExpand"></i> -->

In such case what could happen is that your click is bound to a now non-existent element .在这种情况下,可能发生的情况是您的点击绑定到一个现在不存在的元素

  • Your <i class="scoreCardExpand"> get commented out你的<i class="scoreCardExpand">被注释掉
  • Your JS tries to assign click to .scoreCardExpand (that does not exists)您的 JS 尝试将点击分配给.scoreCardExpand (不存在)
  • Font awesome creates a <svg class="scoreCardExpand" Font <svg class="scoreCardExpand"创建了一个<svg class="scoreCardExpand"
  • The click event does not work.单击事件不起作用。

The only other viable solution I would try is to assign the click event to some <i> 's parent element ,我会尝试的唯一其他可行的解决方案是将 click 事件分配给某些<i>父元素
or to use click handler with delegation:或者使用带有委托的点击处理程序:

jQuery(function($) {

  $(document /* or rather some parent ID */).on("click", ".scoreCardExpand", function() {  
    console.log(5);
  });

});

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

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