Home > Article > Web Front-end > How jQuery gets the value of a special attribute and sets the content
jQuery gets the value of special attributes and sets the content: 1. Set and get the value of the form element through the val method; 2. Get the value of the content through the html method; 3. Get and set the content through the text method value.
The operating environment of this article: Windows 7 system, jquery version 3.2.1, Dell G3 computer.
This article introduces the use of jQuery special attribute methods and how to set the value of elements. It has certain reference value and I hope it will be helpful to everyone.
val method
The val method is suitable for setting and getting the value of form elements, such as the value of input and textarea
//获取name的值 $("#name").val(""); //设置name的值 $("#name").val("张三");
html method
html method is equivalent to innerHTML and will recognize html tags
//获取内容的值 $("div").html("<span>今天天气真好</span>");
text method
The text method is equivalent to innerText and does not recognize html tags
//获取内容的值 $("div").text(); //设置内容的值 $("div").text("<span>今天天气真好</span>");
The difference between innerHTML and innerText
innerText What is printed is the text information between tags, which will filter out the tags and is not supported by lower versions of Firefox.
innerHtml Prints the content between tags, including tags and text information, and is supported by all browsers
width method and height method
The two methods are the same
$(“img”).width(200);//设置图片的宽度 $(“img”).width();//获取图片的宽度; 例如获取可视区的宽高 $(window).width(); $(window).height();
[Note]When the content in () is empty, it means getting the value of the content. When there is a value, it means setting the value of the content.
jQuery special attribute method[jQuery Tutorial]
Summary: Through the introduction of this article, you must have learned how to get the text value of an element and set the text content, as well as the visual area The values of width and height, I hope this article will be helpful to everyone's learning.
The above is the detailed content of How jQuery gets the value of a special attribute and sets the content. For more information, please follow other related articles on the PHP Chinese website!