将当前网址作为变量传递给jquery [英] Pass current url as variable to jquery
问题描述
很抱歉,如果这已经得到解答,但我找不到它。
我试图选择与当前网址的链接,因为它是链接的href,并展开它的父母。
Html:
< ul class ='product-categories'>
< li>< a href ='http://example.com/link1'>链接1< / a>
< ul>
< li>< a href ='http://example.com/link/sub-link1'>子链接1< / a>< / li>
< li>< a href ='http://example.com/link/sub-link2'> Sub link 2< / a>< / li>
< li>< a href ='http://example.com/link/sub-link3'>子链接3< / a>< / li>
< / ul>
< / li>
< li>< a href ='http://example.com/link2'> Link 2< / a>< / li>
< li>< a href ='http://example.com/link3'> Link 3< / a>< / li>
< li>< a href ='http://example.com/link4'> Link 4< / a>< / li>
< / ul>
Jquery:
var url = document.URL;
$('a [href = url]')。parentsUntil('ul.product_categories')。slideToggle('fast');
由于某些原因,如果我将url直接粘贴到函数中,但不起作用一个变量?我尝试使用window.location.href而不是,相同的结果。也提醒了网址的内容,这很好。
连接到函数中具有相同的结果。
无效:
$('a [href ='+ url +]')。parentsUntil('ul.product_categories')。slideToggle('fast');
如果我将url直接粘贴到函数中,它可以工作
$('a [href =http://example.com/product-category/bamboo-cutting-board/round-cutting-board/]' ).parentsUntil( 'ul.product_categories')的slideToggle( '快')。
字符串连接:
$('a [href =''+ url +'''')
Sorry if this has already been answered, but I couldn't find it.
I'm trying to select the link with the current url as it's href in a ul of links, and expand it's parents.
Html:
<ul class ='product-categories'>
<li><a href ='http://example.com/link1'>Link 1</a>
<ul>
<li><a href ='http://example.com/link/sub-link1'>Sub link 1</a></li>
<li><a href ='http://example.com/link/sub-link2'>Sub link 2</a></li>
<li><a href ='http://example.com/link/sub-link3'>Sub link 3</a></li>
</ul>
</li>
<li><a href ='http://example.com/link2'>Link 2</a></li>
<li><a href ='http://example.com/link3'>Link 3</a></li>
<li><a href ='http://example.com/link4'>Link 4</a></li>
</ul>
Jquery:
var url = document.URL;
$('a[ href = url ]').parentsUntil('ul.product_categories').slideToggle('fast');
For some reason this works if I paste the url directly into the function, but doesn't work with a variable? I've tried using window.location.href instead, same result. Have also alerted the contents of url, which was fine.
Concatenating it into the function has the same result.
Doesn't work:
$('a[href= " + url + "]').parentsUntil('ul.product_categories').slideToggle('fast');
If I paste the url directly into the function, it works
$('a[href= "http://example.com/product-category/bamboo-cutting-board/round-cutting-board/"]').parentsUntil('ul.product_categories').slideToggle('fast');
String concatenation:
$('a[href="' + url + '"]')
这篇关于将当前网址作为变量传递给jquery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!