Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript jQuery不管if语句如何,每个函数都会迭代所有内容_Javascript_Jquery - Fatal编程技术网

Javascript jQuery不管if语句如何,每个函数都会迭代所有内容

Javascript jQuery不管if语句如何,每个函数都会迭代所有内容,javascript,jquery,Javascript,Jquery,代码在单击页面上的特定链接后附加页面URL。当我运行代码时,它会按预期执行,只将URL附加到与代码匹配的链接。然而,当我打印到控制台时,我注意到它似乎对页面上的每个链接都这样做,尽管if语句应该限制它。当我取出if语句时,代码的功能是相同的。然而,即使它可以工作,我还是想让它更高效,并且只在匹配参数的链接上运行 // Function to change the link by adding the current page function setSlateLink() { //

代码在单击页面上的特定链接后附加页面URL。当我运行代码时,它会按预期执行,只将URL附加到与代码匹配的链接。然而,当我打印到控制台时,我注意到它似乎对页面上的每个链接都这样做,尽管if语句应该限制它。当我取出if语句时,代码的功能是相同的。然而,即使它可以工作,我还是想让它更高效,并且只在匹配参数的链接上运行

 // Function to change the link by adding the current page
 function setSlateLink() {
     // get the URL of the current page
     var currentPage = document.location.href;
     // get the URL of the slate
     var slateLink = jQuery('a[href$="code=magic"]').attr("href");
     // rewrite the slate, adding the query string with a key for Slate and value of the current page
     console.log("Processing link change");
     return slateLink += "&sys:interaction:summary=" + currentPage;

 }

 jQuery(document).ready(function() {

     jQuery("a").each(function(i, item) {
         // if there is an a tag, check that it ends like this
         if (jQuery(i == 'a[href$="code=magic"]')) {
             console.log("Found link: " + jQuery("a").attr("href"));
             // change the link to set the referrer (current page) in the URL
             jQuery('a[href$="magic"]').attr("href", setSlateLink());
             console.log("Changed link");
         }

     });
 });
你知道这是什么原因吗?

你打电话给
if(jQuery(i='a[href$='code=magic]')

这将返回一个真实的jquery对象,使if语句始终执行

如果您试图查看该元素是否与选择器匹配,可以使用jquerys
#is
方法:
if($(item).is('a[href$=“code=magic”])

您调用的
if(jQuery(i='a[href$=“code=magic”])

这将返回一个真实的jquery对象,使if语句始终执行

如果您试图查看该元素是否与选择器匹配,可以使用jquerys
#is
方法:
if($(item).is('a[href$=“code=magic”])

使用
is()
确定元素是否与选择器匹配

例如:

if ($(item).is('a[href$="code=magic"]')) {
  // item matches
}
但您可以通过选择这些特定的a标记来简化它:

$('a[href$="code=magic"]').each(function(i, item) {
  // just code=magic links...
});
再往前走一步

$('a[href$="code=magic"]').each(function(i, item) {
  var href = $(item).attr('href');

  $(item).attr('href', href + '&sys:interaction:summary=' + document.location.href);
});
使用
is()
确定元素是否与选择器匹配

例如:

if ($(item).is('a[href$="code=magic"]')) {
  // item matches
}
但您可以通过选择这些特定的a标记来简化它:

$('a[href$="code=magic"]').each(function(i, item) {
  // just code=magic links...
});
再往前走一步

$('a[href$="code=magic"]').each(function(i, item) {
  var href = $(item).attr('href');

  $(item).attr('href', href + '&sys:interaction:summary=' + document.location.href);
});

您的if将始终为true,因为它的计算结果为空jQuery collectionCode毫无意义,您在循环的每个迭代中都选择了神奇的链接。因此,对同一个链接反复执行此操作。如果始终为真,因为它的计算结果为空jQuery collectionCode毫无意义,您在循环的每次迭代中都选择了神奇的链接。因此,您需要对同一链接反复执行此操作。您还应该提供一个解决方案;)您还应该提供一个解决方案;)