如何获取数组的值并将其与window.locaton匹配

时间:2019-10-04 08:21:42

标签: javascript jquery arrays string-matching location-href

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

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

我尝试删除每个元素上域的第一部分。通过真实的对待他们。并进行替换。但似乎不返回数组。要进行清晰的爆炸,请检查代码。

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>

1 个答案:

答案 0 :(得分:0)

这是您可以使用普通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
    }
})