Home >Web Front-end >Front-end Q&A >How to replace div content with jquery
In jquery, you can use the html() method to replace the content of the div element. This method can set and cover the content of all matching elements. The syntax is "$("div").html("new div content ")".
The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.
In jquery, you can use the html() method to replace the content of the div element.
html() method returns or sets the content (inner HTML) of the selected element.
When a parameter is passed to this method and a value is set, it will overwrite the content of all matching elements.
Syntax:
$(selector).html(content) //或 $(selector).html(function(index,oldcontent))
Parameters | Description |
---|---|
content | Optional. Specifies the new content of the selected element. This parameter can contain HTML tags. |
Parameters | Description |
---|---|
function(index ,oldcontent) |
Specifies a function that returns the new content of the selected element.
|
Example:
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <script src="js/jquery-1.10.2.min.js"></script> <script> $(document).ready(function() { $("button").click(function() { $("div").html("Hello <b>world!</b>"); }); }); </script> </head> <body> <div>这是一段文字。</div> <div>这是另一段文字。</div><br> <button>改变 div 元素的内容</button> </body> </html>
[Recommended learning: jQuery Video tutorial、web front-end video】
The above is the detailed content of How to replace div content with jquery. For more information, please follow other related articles on the PHP Chinese website!