Home  >  Article  >  Web Front-end  >  How to modify the value of child elements in jquery

How to modify the value of child elements in jquery

WBOY
WBOYOriginal
2021-11-19 10:33:153008browse

Jquery method to modify the value of a sub-element: 1. Use the "$("element id value")" statement to obtain the sub-element object according to the specified id value; 2. Use the html() method to modify the obtained sub-element The element object value is sufficient, and the syntax is "child element object.html("modified value");".

How to modify the value of child elements in jquery

The operating environment of this tutorial: windows7 system, jquery3.6.1 version, Dell G3 computer.

jquery method of modifying the value of a child element

We can use the html() method to return or set the content of the selected element (inner HTML) .

If this method does not set parameters, the current content of the selected element is returned.

The syntax is:

$(selector).html(content)

Among them, content is an optional parameter. Specifies the new content of the selected element. This parameter can contain HTML tags.

jquery only needs to use the html method to modify the value of a child element. The syntax is "element object.html (modified value);"

Let's take a look at it through an example:

<html>
<head>
<script type="text/javascript" src="/jquery/jquery.js"></script>
<script type="text/javascript">
$(document).ready(function(){
  $(".btn1").click(function(){
    $("p").html("Hello world!");
  });
});
</script>
</head>
<body>
<p>This is another paragraph.</p>
<button class="btn1">改变 p 元素的内容</button>
</body>
</html>

Output result:

How to modify the value of child elements in jquery

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of How to modify the value of child elements in jquery. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn