Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在jQuery中粗体匹配两个列表框中的项目?_Jquery - Fatal编程技术网

如何在jQuery中粗体匹配两个列表框中的项目?

如何在jQuery中粗体匹配两个列表框中的项目?,jquery,Jquery,我试图在jquery中粗体显示两个列表框之间的匹配项。我尝试了以下代码,但它给了我一个错误 var $itemInBox1 = $("#box1View").find("option"), $itemInBox2 = $("#box2View").find("option"); $itemInBox1.each(function () { var itemInBox1 = this.val(); $itemInBo

我试图在jquery中粗体显示两个列表框之间的匹配项。我尝试了以下代码,但它给了我一个错误

var $itemInBox1 = $("#box1View").find("option"),
        $itemInBox2 = $("#box2View").find("option");

        $itemInBox1.each(function () {
            var itemInBox1 = this.val();
            $itemInBox2.each(function() {
                var itemInBox2 = this.val();

                if (itemInBox1 == itemInBox2)
                {
                    $itemInBox1.css("font-weight","bold");
                }
            });
        });

示例JSIDLE:

尝试对使用正确的语法:

$itemInBox1.css("font-weight","bold");
或:

而不是:

$itemInBox1.css("font-weight:bold");

以下是我的工作:

我确实缩短了高度,这样我可以同时看到两个。Chrome忽略了粗体。但是,您可以在这里看到带有注释的警报的工作代码

$("#box1View option").each(function (i) {
    //alert( $(this).text() + " : " + $(this).val() );

    var opt = $(this).text();
    $('#box2View option:contains('+ opt +')').css("font-weight", "bold");  
    $('#box2View option:contains('+ opt +')').css("color", "red");
});

HTH

谢谢您的更正。但这不是我面临的问题。
TypeError:undefined不是一个函数
您是否包含jQuery或使用了错误包含的插件?你能设置一个演示吗?我已经使用了jquery dualListBox插件。我正在尝试将两个列表框中的匹配项加粗。我认为,如果您已经实现了Felix answer,但仍然失败,我们需要看到一个快速的提琴。我添加了JSFIDDLE。我正在查看这一点,我注意到chrome根本不会加粗列表项。我会的。Chrome将尊重字体颜色。当我运行你的小提琴时,匹配代码从不触发。这就是真正的问题所在吗?问题在这一行
var itemInBox2=this.text()。它给了我一个错误
TypeError:string不是函数
我认为$(this).text()是正确的。请看下面我的解决方案。你可以检查浏览器或其他东西,但我认为这是最好的答案,不会走得太远。谢谢你帮助我。我再问你一个问题。我能看到颜色变为红色,但不加粗。我缺少什么?Chrome忽略了选项文本的粗体:(IE将显示粗体选项。我添加了颜色以使其在两种浏览器中都能工作。看到这一点,我意识到不需要“如果”。
$("#box1View option").each(function (i) {
    //alert( $(this).text() + " : " + $(this).val() );

    var opt = $(this).text();
    $('#box2View option:contains('+ opt +')').css("font-weight", "bold");  
    $('#box2View option:contains('+ opt +')').css("color", "red");
});