-2

我得到了一些列表,其中包含如下链接:

<li class="folder340 collapsable"><a href="http://www.domain.com/title01" class="">   <span>Title01</span></a></li>
<li class="folder360 collapsable"><a href="http://www.domain.com/title02" class=""><span>Title02</span></a></li>

我希望他们成为:

<li class="folder340 collapsable"><a href="http://www.domain.com/340" class="">   <span>Title01</span></a></li>
<li class="folder360 collapsable"><a href="http://www.domain.com/360" class=""><span>Title02</span></a></li>

我知道我应该使用类似的东西:

    var url = $(this).find('a').attr('href') + "this need to be the li class and minus the text folder";

任何的想法?

4

7 回答 7

1

var className = $(".collapsible").attr("class");

然后,您可以使用简单的 JavaScript 字符串函数删除文本“文件夹”。

然后只需将最终域存储在变量中并使用 $("specefic-collapsible").attr("href") = finalURL;

抱歉,如果我的答案格式不正确,但我在手机上。

于 2013-02-28T04:01:42.120 回答
1

尝试

<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript">

    $(function()
    {
        $('li.collapsable').each(function()
        {
            //console.log($(this).attr('class'));
            var cls  = $(this).attr('class').replace('folder',"").replace(" collapsable","");
            var link = $(this).find('a');
            link.attr('href',"http://www.domain.com/"+cls)
            console.log(cls);
        });
    });

</script>
<ul>
    <li class="folder340 collapsable"><a href="http://www.domain.com/title01" class="">   <span>Title01</span></a></li>
    <li class="folder360 collapsable"><a href="http://www.domain.com/title02" class=""><span>Title02</span></a></li>
</ul>
于 2013-02-28T04:01:47.593 回答
1

尝试这样的事情:

$('.collapsable').each(function(){
    //Select some elements
    var $li = $(this);
    var $link = $li.find("a");

    //Grab the number from the class name beginning with 'folder'
    var folderNum = /folder([\d]+)/g.exec($li.attr("class"))[1];

    //Get the text from the span, this will be what we need to replace in the url 
    var titleToReplace = $link.find("span").text().toLowerCase();

    //Get the original URL
    var originalUrl = $link.attr('href');

    //Replace the part of the URL
    var newUrl = originalUrl.replace(titleToReplace, folderNum);

    $link.attr("href", newUrl);
});

在这里工作演示:http: //jsfiddle.net/q4XJs/

于 2013-02-28T04:02:02.113 回答
1

这有效:

$('li a').each(function(){
    var cls = $(this).parent().attr('class').split(' ')[0];

    var folder = cls.split('folder')[1];

$(this).attr('href', 'http://www.domain.com/'+folder);
});

jsfiddle:http: //jsfiddle.net/nD4qL/

于 2013-02-28T04:06:59.243 回答
0

试试这个:- http://jsfiddle.net/aidioo7/jdCVq/

JS:-

$(function(){
    $(".collapsable").each(function(){
        var oldhref=$(this).find("a").attr("href");
        var className = $(this).attr("class");
        className=className.replace("folder","").replace(" collapsable","");
        oldhref=oldhref.replace($(this).find("a").text().toLowerCase().trim(),className);
        $(this).find("a").attr("href",oldhref);
    });
});
于 2013-02-28T04:11:59.237 回答
0

尝试以下操作:

$('li').each(function(){
    var n=$(this).attr('class').split(" ")[0].length;
     var x=$(this).attr('class').substring(n-3,n);
     $(this).find('a').attr('href','http://www.domain.com/'+x);
    });
于 2013-02-28T04:21:22.913 回答
-1
<li class="folder340 collapsable"><a id="link1" href="http://www.domain.com/title01" class="">   <span>Title01</span></a></li>
<li class="folder360 collapsable"><a id="link2" href="http://www.domain.com/title02" class=""><span>Title02</span></a></li>

JS:

$("#link1").attr("href", "http://www.domain.com/340");

$("#link2").attr("href", "http://www.domain.com/360");
于 2013-02-28T04:00:35.133 回答