是否可以用jQuery或Php替换文本? [关闭]

Does anyone know if it is possible to use jQuery or Php to get an element and replace what is in it?

I have some dynamically generated links and I am trying to change what is in them. I think something like this would be possible with an object oriented programming language but I am unsure where to start.

Any pointers appreciated.

Here is an example of what I have for mark-up:

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

Here is an example of what I want to do:

<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>

Using jQuery, you can replace

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

with

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

by calling

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

Well you can use JavaScript (jQuery) to select those links:

<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>

Here is a demo: http://jsfiddle.net/3JBEK/

Simple, use jQuery.html(string)

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

api: jQuery.html()