是否可以用jQuery或Php替换文本?

时间:2011-12-09 02:44:13

标签: php jquery

有没有人知道是否可以使用jQuery或Php来获取元素并替换其中的元素?

我有一些动态生成的链接,我正在尝试更改其中的内容。我认为使用面向对象的编程语言可能会出现类似的情况,但我不确定从哪里开始。

任何指示赞赏。

以下是我用于标记的示例:

<a class="link1>1<a>
<a class="link2>2<a>
<a class="link3>3<a>

以下是我想要做的一个例子:

<a class="link1><p>Custom Text</p><a>
<a class="link2><p>Custom Text Number 2</p><a>
<a class="link3><p>Custom Text Number 3</p><a>

3 个答案:

答案 0 :(得分:2)

您可以使用JavaScript(jQuery)来选择这些链接:

<script>
//setup custom text for each link
var custom_text = ['Custom Text', 'Custom Text Number 2', 'Custom Text Number 3'];

//select all links that class starts with `link` and change it's text
$('a[class^="link"]').each(function (index, obj) {
    $(this).text(custom_text[index]);
});
</script>

以下是演示:http://jsfiddle.net/3JBEK/

答案 1 :(得分:1)

使用jQuery,您可以替换

<a class="link1">1</a>

<a class="link1"><p>Custom Text</p></a>

致电

$('.link1').html('<p>Custom Text</p>');

答案 2 :(得分:1)

简单,使用jQuery.html(string)

例如$('.link1').html('<p>Custom Text</p>')

api:jQuery.html()

相关问题