Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/390.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 将url与变量中的文本进行比较_Javascript_Jquery - Fatal编程技术网

Javascript 将url与变量中的文本进行比较

Javascript 将url与变量中的文本进行比较,javascript,jquery,Javascript,Jquery,我有一个名为links的变量,它存储以前访问过的任何URL 我需要将页面上的URL与变量的结果相匹配。如果它们匹配,则仅将类“已访问”分配给这些链接 例如,如果我的页面有: <a href="link1.html"> <a href="link2.html"> <a href="link3.html"> <a href="link4.html"> <a href="link5.html"> 在本例中,我们希望将类“visited”添加

我有一个名为links的变量,它存储以前访问过的任何URL

我需要将页面上的URL与变量的结果相匹配。如果它们匹配,则仅将类“已访问”分配给这些链接

例如,如果我的页面有:

<a href="link1.html">
<a href="link2.html">
<a href="link3.html">
<a href="link4.html">
<a href="link5.html">
在本例中,我们希望将类“visited”添加到存储在变量link1.html、link3.html和link4.html中的链接中。有大量的链接和各种各样的文本。这些只是一个简单的例子

这就是我到目前为止所做的:

$(document).ready(function() {
    $('#rightbox li a').each(

    function(intIndex){
        var links = $(this).attr('href');
        console.log(links);
        $.ajax({
            url: links,
            type:'HEAD',
            error: function() { },
            success: function() {
                var visited = (links)
                $("#visitedLinkContainer").append(visited);

                $(this).attr('href').addClass("visited");`
                // I tried this but it adds visited to everylink which I knew would, but I don't know what to put here`
            }
        });                     
    });
});
这是一个个人项目在家里。目前我正在使用本地存储,但如果可能的话,我更愿意这样做


感谢您提供的任何帮助使用以下代码:

links.forEach(function(link) {
  $('#rightbox li a[href="' + link + '"]').addClass('visited');
});

jQuery选择器选择其
等于
的元素,并使用以下代码:

links.forEach(function(link) {
  $('#rightbox li a[href="' + link + '"]').addClass('visited');
});

jQuery选择器选择其
等于

的元素,将此行[
var links=$(this).attr('href');
]转换为:

var LINK = $(this), 
    links = LINK.attr('href');
现在,在第
$(“#visitedLinkContainer”).append(visited)行之后放在此处而不是您的代码:

LINK.addClass("visited");
或者试试这个:

$("#rightbox [href='"+links+"']").addClass("visited");

将此行[
var links=$(this).attr('href');
]转换为:

var LINK = $(this), 
    links = LINK.attr('href');
现在,在第
$(“#visitedLinkContainer”).append(visited)行之后放在此处而不是您的代码:

LINK.addClass("visited");
或者试试这个:

$("#rightbox [href='"+links+"']").addClass("visited");
for(var i=0;i
for(var i=0;i我创建了一个示例:

html

<a href="#1">Link1</a>
<a href="#2">Link2</a>
<a href="#3">Link3</a>
<a href="#4">Link4</a>
<a href="#5">Link5</a>
<a href="#2">Link2</a>
JS

.visited {
  color: red;
}
$(document).ready(function() {

// Faking that you already entered the first 2 links
var visited = ['#1', '#2'];

// when clicking it adds the class visited right away
  $( "a" ).click(function() {
    visited.push($(this).attr('href'));
    $(this).addClass( "visited" );
  });

// loop trough the visited url and find the corresponding a tags that all have the urls inside the visited variable
  $.each(visited, function( index, value ) {
    // this gets all links with that same value, if you don't want this you need to store something unique of the a tag or the entire element inside the var visited 
    var allLinks = $('a[href^="' + value + '"]');
    $.each(allLinks, function() {
       $(this).addClass( "visited" )
    })
    // this allert shows you what index and value are
    alert( index + ": " + value );
  });

});
希望这能有所帮助

我创建了一个示例:

html

<a href="#1">Link1</a>
<a href="#2">Link2</a>
<a href="#3">Link3</a>
<a href="#4">Link4</a>
<a href="#5">Link5</a>
<a href="#2">Link2</a>
JS

.visited {
  color: red;
}
$(document).ready(function() {

// Faking that you already entered the first 2 links
var visited = ['#1', '#2'];

// when clicking it adds the class visited right away
  $( "a" ).click(function() {
    visited.push($(this).attr('href'));
    $(this).addClass( "visited" );
  });

// loop trough the visited url and find the corresponding a tags that all have the urls inside the visited variable
  $.each(visited, function( index, value ) {
    // this gets all links with that same value, if you don't want this you need to store something unique of the a tag or the entire element inside the var visited 
    var allLinks = $('a[href^="' + value + '"]');
    $.each(allLinks, function() {
       $(this).addClass( "visited" )
    })
    // this allert shows you what index and value are
    alert( index + ": " + value );
  });

});

希望这能帮助你用类
已访问的
标记已访问的链接。我认为这是ajax成功函数。我尝试了你的答案,但我得到了
链接。forEach不是一个函数
你的
链接
变量不是一组URL,而是一个变量。你应该改变你的代码:
var-links=[];$('#rightbox li a').each(函数(intIndex){links.push($(this.attr('href'));})
。非常感谢,还有很多东西要学。它现在很好用。你想用类
已访问的
标记已访问的链接。我想这是在你的ajax成功函数中。我尝试了你的答案,但我得到了
链接。forEach不是一个函数
你的
链接
变量不是一排URL,而是一个变量。你应该更改您的代码:
var links=[];$('#rightbox li a')。每个(函数(intIndex){links.push($(this.attr('href'));});
。非常感谢您,还有很多东西需要学习。现在它工作得很好。