如何找到href与网址匹配

时间:2019-03-06 22:45:14

标签: javascript jquery

如何选择-- test str = "hello world!" strbyte = strToBytes(str) bytestr = 'return bytesToStr(' .. strbyte .. ')' strBack = string.dump(load(bytestr),true,true) print(strbyte) print(bytestr) print(load(strBack)()) ,使其结尾href与从URL提取的两个数组的userID和adminID匹配?我正在尝试以下操作,但不起作用。

<a>

1 个答案:

答案 0 :(得分:0)

选择器中缺少用户ID,并且需要在值周围加上括号。下面的工作示例:

// Anchor elements implement Location object, makes for a good mock
const mockLocation = document.createElement('a');
mockLocation.href = 'http://www.foobar.com/admin/123/user/123/';

const pathnameParts = mockLocation.pathname.split('/');

const adminId = pathnameParts[2];
const userId = pathnameParts[4];

if (adminId === userId){
  $(`a[href="/user/${userId}"]`).css('color','purple');             
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<a href="/user/123">User 123</a>
<a href="/user/129">User 129</a>