【问题标题】:Warning from PhpStorm: Assigned expression type jQuery is not assignable to type string来自 PhpStorm 的警告:分配的表达式类型 jQuery 不可分配给类型字符串
【发布时间】:2018-10-23 21:42:07
【问题描述】:
$('a').click(function () {
    if ($(this).attr('id') !== '_blank') {
        window.location.href = $(this).attr('href');
    }
});

上面的代码在 PhpStorm 中显示了一条警告消息:

jQuery 赋值表达式类型不能赋值给字符串类型

我该如何解决?

【问题讨论】:

  • 我逐字尝试了您的代码,它似乎工作正常。您确定是这个 sn-p 导致了警告吗?
  • 另外,这是否应该与id="_blank" 建立链接什么都不做?在这种情况下不要忘记添加e.preventDefault(),其中e是回调函数中的事件参数。
  • 等等,你在用 TypeScript 吗?
  • 我在 phpstorm 中编码,警告信息没有显示在 chrome 中。

标签: javascript jquery html phpstorm


【解决方案1】:

此代码与 Chrome 完美搭配。

<html>
<head>
<script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>

</head>
<body>
    <a href="https://www.google.com">Google</a>
    <script>
        (function(){
            $('a').click(function () {
             if ($(this).attr('id') !== '_blank') {
               window.location.href = $(this).attr('href');
             }
            });
        })();

</script>

希望对你有帮助

【讨论】:

    猜你喜欢
    • 2020-02-24
    • 2016-08-10
    • 2022-01-17
    • 2021-02-22
    • 2020-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多