首页 > 解决方案 > 如何获取数组的值并将其与 window.locaton 匹配

问题描述

我想在循环中删除部分字符串,然后我必须检查它是否等于 window.location。

linkAdd= 应该返回一个数组。但它看起来像它返回一个字符串。那么如何检查网站上是否有等于 adresPath var 的点赞?如何创建一个包含所有 /123/1.php、/123/2.php、/123/3.php 而不仅仅是一个 /123/1.php 的 var?然后检查/123/1.php是否等于adresPath变量?

我试图删除每个元素上域的第一部分。通过真正的他们。并做.replace。但它似乎没有返回一个数组。要获得清晰的解释,请检查代码。

var adresPath = window.location.pathname; 
var link      = $('.menu-item>a');
var linkAdres = $('.menu-item>a').href;
var linkAdd   =  $('.menu-item>a').href.replace("http://bertconinx.com/","")
//link linkAdres and linkadd should be arrays. but when i try linkadd[i] it returns a letter of the string.

//ther are 19 a href in my code.


for(i = 0; i < link.length; i++){
    var linkAdd   = link[i].href.replace("http://bertconinx.com/","")
    };

> linkAdd= should return a array. but it looks like its return a string.
> So how do I check if there is a like on the website that is equal to
> the adresPath var? how do I create a var that contians all
> /123/1.php,/123/2.php,/123/3.php, and not just one /123/1.php? And
> then check /123/1.php is equal to the adresPath variable?


if(adresPath=linkAdd[i]){alert(happy happy it works!)};```

> linkAdd= should return a array. but it looks like its return a string.
> So how do I check if there is a like on the website that is equal to
> the adresPath var? how do I create a var that contians all
> /123/1.php,/123/2.php,/123/3.php, and not just one /123/1.php? And
> then check /123/1.php is equal to the adresPath variable?
> 
> error messegas are.. 
> 
> undefined when tying to return an object of the array I get the letter
> of the string. ex. link[9] retunrs "p" of php.




the html

<pre>

    <li id="menu-item-461" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-461">

        <a href="http://bertconinx.com/category/portriats/">portriats</a>
        <ul class="sub-menu">
            <li id="menu-item-473" class="menu-item menu-item-type-post_type menu-item-object-post current-menu-item menu-item-473">
                        <a href="http://bertconinx.com/2019/08/12/non-profit-profit/" aria-current="page">Disarray Body</a></li>
            <li id="menu-item-617" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-617">
                    <a href="http://bertconinx.com/2019/09/16/girls/">#Girls</a></li>
        </ul>
</li>

<li id="menu-item-462" class="menu-item menu-item-type-taxonomy menu-item-object-category current-post-ancestor current-menu-ancestor current-menu-parent current-post-parent menu-item-has-children menu-item-461">

        <a href="http://bertconinx.com/category/portriats/">Item2</a>
        <ul class="sub-menu">
            <li id="menu-item-412" class="menu-item menu-item-type-post_type menu-item-object-post current-menu-item menu-item-473">
                        <a href="http://bertconinx.com/2019/08/12/non-profit-profit/" aria-current="page">object1</a></li>
            <li id="menu-item-619" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-617">
                    <a href="http://bertconinx.com/2019/09/16/girls/">Object2</a></li>
        </ul>
</li> </pre>

标签: javascriptjqueryarraysstring-matchinglocation-href

解决方案


这是你可以用普通的 javascript 做的事情

const links = document.getElementsByClassName('menu-item');
const anchors = [];
for (let i = 0; i < links.length; i++) {
    anchors.push(links[i].getElementsByTagName('a')[0]); 

}

const linkAddresses = [...anchors].map(anchor => anchor.href.replace("http://bertconinx.com/",""));

现在你将["category/portriats/", "2019/08/12/non-profit-profit/", "2019/09/16/girls/", "category/portriats/", "2019/08/12/non-profit-profit/", "2019/09/16/girls/"]在你的linkAddress变量中有一个数组。现在您可以遍历此数组并查找是否有任何链接与addresPath

linkAddresses.forEach(address => {
    if (address === adresPath) {
        // matches
    }
})

推荐阅读