【问题标题】:how to get the value of an array and match it to the window.locaton如何获取数组的值并将其与 window.location 匹配
【发布时间】:2020-02-02 13:30:24
【问题描述】:

我想在循环中删除部分字符串,然后我必须检查它是否等于 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>

【问题讨论】:

  • 你能在html部分添加几行链接吗?
  • 你能添加 html sn-p 吗?以便问题可以重现?
  • 请发布您的 HTML 代码的相关部分
  • 粘贴在html中。
  • 如何将adresPath拆分成逗号上的数组,并一一处理? adresPath.split(",") 然后可以匹配到绝对或相对路径...

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


【解决方案1】:

这是你可以用普通的 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/",""));

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-21
    • 1970-01-01
    • 2019-06-23
    • 2019-07-26
    相关资源
    最近更新 更多