用于在客户端上操作IU的jQuery

用于在客户端上操作IU的jQuery,jquery,html-lists,Jquery,Html Lists,我开始学习jQuery编程,并希望在编写这段代码时获得帮助 有一个包含多个li项目的ul列表。每个li具有以下结构 <li><a href="http://www.findadeal.com/city-state/"/>City</li> 城市 例如,城邦和城市是任何城市的硬编码值 <li><a href="http://www.findadeal.com/buffalo-ny/"/>Buffalo</li> 水

我开始学习jQuery编程,并希望在编写这段代码时获得帮助

有一个包含多个li项目的ul列表。每个li具有以下结构

 <li><a href="http://www.findadeal.com/city-state/"/>City</li> 
  • 城市
  • 例如,城邦和城市是任何城市的硬编码值

     <li><a href="http://www.findadeal.com/buffalo-ny/"/>Buffalo</li>
    
    水牛城 请求页面时,将以查询字符串的形式发送产品分类参数,如“beer”

    在页面加载期间,我需要修改li链接以获取以下表单

     <li><a href="http://www.findadeal.com/buffalo-ny/beer/"/>Buffalo Beer</li>
    
  • 水牛啤酒
  • 如何使用jQuery对循环逻辑进行编码,以便相应地修改每个li元素。一页上通常不超过50个城市


    谢谢。

    在jQuery中,您可以将
    text()
    函数传递给另一个函数,该函数的返回值将是当前元素的新值

    //wait for document.ready to fire before doing anything
    $(function () {
    
        //check to see if there is a query-string or not
        if (window.location.search != '') {
    
            //split the query-string by ampersands
            var parameters = window.location.search.replace('?', '').split('&');
    
            //iterate through the query-string key/value pairs
            for (var i = 0, len = parameters.length; i < len; i++) {
    
                //get the key/value pair split into an array
                var pair = parameters[i].split('=');
    
                //if the key is the correct one then we can proceed
                if (pair[0] == 'product-classification') {
    
                    //update each `li` element with the value of the `product-classification` query-string variable
                    $('li').text(function () {
    
                        //add the value to the current text of the `li` element
                        return $(this).text() + ' ' + pair[1];
                    });
    
                    //now we can stop the loop because we have done what we want, if there are lots of query-string parameters this will be a good idea
                    break;
                }
            }
        }
    });
    
    //等待document.ready启动,然后再执行任何操作
    $(函数(){
    //检查是否存在查询字符串
    如果(window.location.search!=''){
    //按符号拆分查询字符串
    var参数=window.location.search.replace('?','').split('&');
    //遍历查询字符串键/值对
    对于(变量i=0,len=parameters.length;i
    下面是一个演示:


    .text()
    的文档:

    我不完全确定您要找的是什么,但我想这可能会让您上路

    $('li').each(function(){ //grab all li elements and loop through them
       var $this = $(this); //capture the jquery object for use later
       $this.find('a').attr('href','your new text');
    });
    

    谢谢你的回复。将href值分配给元素可以正常工作,但由于某些原因,最终的href不是我所期望的。我将为此创建一个新的帖子。