jQuery-如果URL匹配href,则将LI移到UL的顶部? [英] jQuery - If URL matches href then move LI to top of UL?

查看:85
本文介绍了jQuery-如果URL匹配href,则将LI移到UL的顶部?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

曾经试图使它无法成功工作.任何帮助将不胜感激.

Been trying to get this to work unsuccessfuly. Any help would be appreciated.

搜索UL,如果任何href等于当前URL,则将其列表项移至UL列表堆栈的顶部.

Search UL, if any href are equal to the current URL then move it's list item to the top of the UL list stack.

var url = window.location.toString();

$('#partnerCharities li.panel h1 a').each(function(){
   var myHref= $(this).attr('href');
   if( url.match( myHref)) {
        $(this).parent().parent().parent().before("#slider1 li:first");
   }
});

这是一个动态创建的列表(图像库),因此不确定是否是jQuery无法正常工作的原因吗?这是您在Firebug中看到的粗略布局...

It's a dynamically created list (image gallery), so not sure if that's why the jQuery isn't working? This is the rough layout you see in Firebug...

<ul id="slider">
 <li class="panel cloned"></li>
 <li class="panel">
  <article>
   <h1><a href="site.com">Example</a></h1>
  </article>
 </li>
 <li class="panel cloned"></li>
</ul>

推荐答案

您的代码有几个错误,请看我做的一个有效示例.

There are several mistakes with your code, take a look at a working example i made.

JSFiddle

html:

<ul id="slider">
 <li class="panel cloned">test1</li>
 <li class="panel">
  <article>
   <h1><a href="site.com">This should come out on top.</a></h1>
  </article>
 </li>
 <li class="panel cloned">test2</li>
</ul>

js:

var url = "site.com"; //or window.location.toString();

$('#slider li.panel h1 a').each(function(){
   var myHref= $(this).attr('href');
   if( url == myHref) {
        $(this).closest("li").prependTo("#slider");
   }
});

这篇关于jQuery-如果URL匹配href,则将LI移到UL的顶部?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆