仅更改包含img的li中的文本

编程入门 行业动态 更新时间:2024-10-28 11:22:58
本文介绍了仅更改包含img的li中的文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有这个html代码

<ul class="criteria"> <li id="1"> criteria 1 <img id="1" class="save_criterion" src="../images/icons/add-icon-2.png"> <img id="1" class="delete_criterion" src="../images/icons/icon_delete_16x16.png"> </li> <li id="2"> criteria 2 <img id="2" class="save_criterion" src="../images/icons/add-icon-2.png"> <img id="2" class="delete_criterion" src="../images/icons/icon_delete_16x16.png"> </li> </ul>

我具有动态更改li文本的功能

I have function to change li text dynamically

$("input#criterion_name").live("keyup",function(e){ var newText = $("input#criterion_name").val(); if(newText){ $("ul.criteria > li.selected").html(newText); } else{ // can't leave the text filed empty $("#criterion_name").addClass("error"); }

问题是当我在输入文本中键入文本时,li元素中的图像被删除了. 我只想更改li元素中的文本

the problem is when I type text in the input text, the images in li elements removed. I want just to change the text in li element

推荐答案

您应将文本包装在其自己的元素中,例如span.然后,您可以像这样定位目标:

You should wrap the text in its own element, like a span. Then you can target that like this:

$("ul.criteria > li.selected > span.thetext").html(newText);

此外,id属性不应仅是数字,并且在文档中不应被多次使用,即,它应该是唯一的.您有几个实例,其中id="1"

Also, an id attribute should not be only numbers and should not be used more than once in a document, i.e. it should be unique. You have several instances where id="1"

更多推荐

仅更改包含img的li中的文本

本文发布于:2023-10-29 20:24:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1540647.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文本   img   li

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!