1

我想用 jquery / javascript 替换文本,但要注意:文本是项目符号格式。

HTML 标记将是:

<ul id="text_to_be_replaced">
  <li>list item 01</li>
  <li>list item 02</li>
  <li>list item 03</li>
</ul>

但我不知道如何在 jquery / javascript 中使用它,除了给每个“li”一个 id。我想做这样的事情:

function change_text(){
  $("#text_to_be_replaced").text("//I want to format the text in here to
  display in bullet format.");
}

任何建议将不胜感激!:)

4

6 回答 6

0

循环每里?

$('#text_to_be_replaced > li').each(function(i) {
   //Do your thang

   //$(this) refers to each of the li being iterated
   $(this).html('new text');

   //i is the iteration index, 0-indexed
});
于 2013-04-26T09:36:06.050 回答
0

那么你可以使用 html 而不是 text

$("#text_to_be_replaced").html("//I want to format the text in here to
  display in bullet format.");
于 2013-04-26T09:36:19.957 回答
0

试试这个

$('#text_to_be_replaced li').each(function() {
    $(this).html('test');
});
于 2013-04-26T09:36:49.333 回答
0

.html()如果要一次性传递新内容,可以使用该方法

function change_text(){
  $("#text_to_be_replaced").html("<li> item 1</li><li>item 2</li><li>item 3</li>");
}
于 2013-04-26T09:39:14.777 回答
0
$('#text_to_be_replaced').children('li').each(function(){
     $(this).html('replaced')
});

使用html()children()

于 2013-04-26T09:43:40.290 回答
0

尝试

function change_text(){
    $("#text_to_be_replaced > li").html(function(index, value){
        return value.replace(/list\s+/, '')
    });
}

演示:小提琴

于 2013-04-26T09:47:00.483 回答