-4

我有一个列表中的项目列表

  • 项目一
  • 项目二
  • 另一项
  • 我还从 URL 中获取了一个变量,格式如下:

    Item-two
    

    选择匹配项目的最佳方法是什么?

    现在,我使用 URL 变量并去掉连字符以复合成一个单词。

    $item= str_replace('-', '', $_GET['item']);
    

    那么,现在,我是否需要遍历列表并删除空格/破折号并进行匹配?

    var getItem = '".$item."';    
    
    if (getTerm != '') {
    
        $('#itemList li').each(function() {
            var that = $(this).replace(/[\\. ,:-]+/g,'');
            if (getTerm  == that) {
                // found selector -- do something
            }
        });
    
    } else {
        // do something else
    }
    

    它对我来说很合适,但我没有得到找到匹配元素的预期结果......我错过了什么?

    4

    1 回答 1

    1

    假设getTerm在混合 js php 范式中正常工作。您应该能够像这样找到元素:

    // instead of "Itemtwo" or "Item-two" 
    // lets make it match exactly as in "Item two"
    getTerm = getTerm.replace('-',' '); 
    
    $('#itemList li').each(function() {
        var that = $(this);
        if (getTerm  == that.text()) {
            that.css('color','red'); // for example
        }
    });
    
    于 2013-10-29T18:15:44.783 回答