使用jQuery在动态菜单中设置li的bg图像

使用jQuery在动态菜单中设置li的bg图像,jquery,image,dynamic,html-lists,Jquery,Image,Dynamic,Html Lists,请容忍我,我刚刚开始使用js/jQuery 我有一个动态生成的菜单,希望根据链接文本将背景图像应用到每个li。 我可以通过.text获得文本,插入图像也没有问题-我只需要找到脚本停止并在每个li处插入相应的图像。 我希望。每个都能这样做,但下面的代码贯穿其中,然后将最后一个li的图像应用于所有这些图像 <script type="text/javascript"> $(function() { $('#CategoryList li a').each(func

请容忍我,我刚刚开始使用js/jQuery

我有一个动态生成的菜单,希望根据链接文本将背景图像应用到每个
li
。 我可以通过
.text
获得文本,插入图像也没有问题-我只需要找到脚本停止并在每个
li
处插入相应的图像。 我希望
。每个
都能这样做,但下面的代码贯穿其中,然后将最后一个
li的图像应用于所有这些图像

<script type="text/javascript">
    $(function() {
        $('#CategoryList li a').each(function() {               
            $('#CategoryList li').css('background','url(http://my_site.com/images/'+ (this).text +'.png) no-repeat');
        });
    });                            
</script>

$(函数(){
$('#CategoryList li a')。每个(函数(){
$('#CategoryList li').css('background','url(http://my_site.com/images/“+(this.text+'.png)不重复”);
});
});                            
很明显,我遗漏了一些基本的东西(可能是数组?)。感谢所有帮助。

使用
$(this).text()代替
(this).text

 $('#CategoryList li').css('background','url(http://my_site.com/images/'+ $(this).text() +'.png) no-repeat');
根据我的理解,你需要

$(function () {
    $('#CategoryList li a').each(function () {
        $(this).closest('li').css('background', 'url(http://my_site.com/images/' + $(this).text() + '.png) no-repeat');
    });
});
使用
$(this).text()
这将返回当前
元素文本

试试这个

 $('#CategoryList li').css('background','url(http://my_site.com/images/'+ $(this).text() +'.png) no-repeat');
(此).text
不起作用,请放入
$(此).text()


closeset
的拼写错误必须是
最近的
感谢您提供了最完整的答案-将此标记为已解决。@用户3514477,您需要将其作为答案接受。这本身并没有解决问题,但感谢您的答复。与
结合使用。最近的
原始的
(此)。文本确实有效,但是很明显,使用正确的语法是有原因的,所以谢谢你的回答。
$(function() {
    $('#CategoryList li a').each(function() {               
        $('#CategoryList li').css('background','url(http://my_site.com/images/'+ $(this).text() +'.png) no-repeat');
    });
});