How to insert new HTML code into existing page?
imagine you have simple page like this:
Hi everybody. when you click here new text will be inserted.
after c开发者_如何学Click :
Hi everybody. when you "ulala ... I am inserted text" new text will be inserted.
- is it possible to insert this text using PHP or Javascript?
- If only javascript is appropriate may you provide a sample code?
thanx a lot
It's easiest with jQuery, but you can use Javascript DOM methods for that.
To make a link happen something, use onClick="js_something()"
. And to insert text use $('#id').text('new text')
.
PHP comes only into play if that new text is to be retrieved from a server resource (url). If so use AJAX, or simply .load(".../get_text.php")
in jQuery.
Your HTML:
<p>Hi everybody. when you <span id="mytext"><a href="javascript:EditText()">click here</a></span> new text will be inserted.</p>
Your Javascript:
<script type="text/javascript">
function EditText() {
document.getElementById('mytext').innerHTML = 'ulala... I am inserted text';
}
</script>
In Javascript try following:
function fun(){
document.getElementById("text_area").innerHTML = document.getElementById("text_area").value + "New str";
}
call fun()
on onclick
of "Click here" which has an id text_area
and could be div
or span
or input
or textarea
any other html tag wherever you wanna add new String.
Hope this helps.
精彩评论