search
HomeWeb Front-endJS TutorialHow does jQuery operate on HTML elements and attributes? (Example of detailed code explanation)

The content of this article is to introduce how jQuery operates HTML elements and attributes? (Detailed code explanation example), let everyone understand how jQuery operates elements and attributes. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

1. Get the content and attributes of the HTML element

(1) Get the content: text(), html () and val() methods

nbsp;html>

	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//text() - 设置或返回所选元素的文本内容            
				$("#btnText").click(function() {
					alert($("#myp1").text());
				});
				$("#btnTextSet").click(function() {
					$("#myp1").text(&#39;这是一个美好的日子&#39;);
					alert($("#myp1").text());
				}); //html() - 设置或返回所选元素的内容(包括 HTML 标记)            
				$("#btnHtml").click(function() {
					alert($("#myp1").html());
				});
				$("#btnHtmlSet").click(function() {
					$("#myp1").html(&#39;这是一个<b>神奇的世界啊&#39;);
					alert($("#myp1").html());
				}); //val() - 设置或返回表单字段的值            
				$("#btnVal").click(function() {
					alert($("#myInput1").val());
				});
				$("#btnValSet").click(function() {
					$("#myInput1").val(&#39;好好学习,天天向上&#39;);
					alert($("#myInput1").val());
				});
			});
		</script>
	

	
		<button>text()方法获取内容</button>
		<button>html()方法获取内容</button>
		<button>val()方法获取内容</button><br>
		<button>text()方法设置内容</button>
		<button>html()方法设置内容</button>
		<button>val()方法设置内容</button>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
		<input>
	

(2) Get attributes: attr() method

nbsp;html><meta><title>My Test JQuery</title>
    <script></script>
    <script>    
        $(function(){  
            //attr() 方法用于获取属性值,也用于设置/改变属性值。            $("#btn_attr1").click(function(){
                alert($("#myHref").attr("href"));
            });
            $("#btn_attr2").click(function(){
                $("#myHref").attr("href","https://www.cnblogs.com");
                alert(&#39;超链接属性设置为:&#39;+$("#myHref").attr("href"));
            });
        });    </script>
    <button>attr()方法获取属性</button><br>
    <button>attr()方法设置属性</button>
    <a>超链接</a>

 

2. Add Elements:

append() and prepend() methods, after() and before() methods

nbsp;html>


	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//append() 方法在被选元素的结尾插入内容(仍然该元素的内部)            
				$("#btn_append").click(function() {
					$("#myp1").append(" 是的");
				}); //prepend() 方法在被选元素的开头插入内容(仍然该元素的内部)            
				$("#btn_prepend").click(function() {
					$("#myp1").prepend("我说 ");
				}); //before() 方法在被选元素的开头插入内容            
				$("#btn_before").click(function() {
					$("#myInput1").before("Hello ");
				}); //after() 方法在被选元素的开头插入内容            
				$("#btn_after").click(function() {
					$("#myInput1").after("World ");
				});
				//特别说明:
				//append() 和 prepend() 方法能够通过参数接收无限数量的新元素
				//after() 和 before() 方法能够通过参数接收无限数量的新元素。
				//可以通过 text/HTML、jQuery 或者 JavaScript/DOM 来创建新元素。
				//举例如下:
				/**
				$("#btn_after").click(function(){
				    var txt1="<b>程序员";                    
				    var txt2=$("<i>").text("是厉害的人");     
				    var txt3=document.createElement("<h1>");  
				    txt3.innerHTML="好用的jQuery!";         
				    $("#myInput1").after(txt1,txt2,txt3);
				});
				**/
			});
		</script>
	

	
		<button>append()方法</button>
		<button>prepend()方法</button><br>
		<button>before()方法</button>
		<button>after()方法</button>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
		<input>

## 3. Delete elements:

remove() method, empty() method

nbsp;html>


	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//remove() 方法删除被选元素及其子元素            
				$("#btn_remove").click(function() {
					$("#myp1").remove();
				}); //empty() 方法删除被选元素的子元素。            
				$("#btn_empty").click(function() {
					$("#myp2").empty();
				});
			});
		</script>
	

	
		<button>remove()方法</button>
		<button>empty()方法</button><br>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
	

4. Get and set CSS classes:

addClass() method, removeClass() method, toggleClass() method

nbsp;html>


	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//addClass() - 向被选元素添加一个或多个类           
				$("#btn_addClass").click(function() {
					$("#myp1").addClass(&#39;blue&#39;);
				}); //removeClass() - 从被选元素删除一个或多个类            
				$("#btn_removeClass").click(function() {
					$("#myp1").removeClass(&#39;blue&#39;);
				}); //toggleClass() - 对被选元素进行添加/删除类的切换操作            
				$("#btn_toggleClass").click(function() {
					$("#myp1").toggleClass(&#39;blue&#39;);
				});
			});
		</script>
	
	<style>
		.blue {
			font-size: 16px;
			background-color: yellow;
		}
	</style>

	
		<button>addClass()方法</button><br>
		<button>removeClass()方法</button><br>
		<button>toggleClass()方法</button>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
	

##5. css() method:

Return CSS properties, set CSS properties, set multiple CSS properties

nbsp;html>

	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//返回指定的 CSS 属性的值            
				$("#btn_css1").click(function() {
					alert(&#39;myp1的背景颜色:&#39; + $("#myp1").css("background-color"));
				}); //设置指定的 CSS 属性            
				$("#btn_css2").click(function() {
					$("#myp1").css("background-color", "green");
				}); //设置多个 CSS 属性            
				$("#btn_css3").click(function() {
					$("#myp1").css({
						"background-color": "pink",
						"font-size": "20px"
					});
				});
			});
		</script>
	

	
		<button>获取css属性的值</button><br>
		<button>设置css属性</button><br>
		<button>设置多个css属性</button><br>
		<p>这是一个神奇的 <b>世界</b>啊 </p>
	

6. Important methods for handling size:

width() and height() methods, innerWidth() and innerHeight () method, outerWidth() and outerHeight() methods

nbsp;html>


	
		<meta>
		<title>My Test JQuery</title>
		<script></script>
		<script>
			$(function() {
				//width() 方法设置或返回元素的宽度(不包括内边距、边框或外边距)。
				//height() 方法设置或返回元素的高度(不包括内边距、边框或外边距)。
				//innerWidth() 方法返回元素的宽度(包括内边距)。
				//innerHeight() 方法返回元素的高度(包括内边距)。
				//outerWidth() 方法返回元素的宽度(包括内边距和边框)。
				//outerHeight() 方法返回元素的高度(包括内边距和边框)。            
				$("#btn_css1").click(function() {
					$("#myp2").html("width: " + $("#myp1").width());
					$("#myp2").html($("#myp2").html() + "<br/>height: " + $("#myp1").height());
					$("#myp2").html($("#myp2").html() + "<br/>innerWidth: " + $("#myp1").innerWidth());
					$("#myp2").html($("#myp2").html() + "<br/>innerHeight: " + $("#myp1").innerHeight());
					$("#myp2").html($("#myp2").html() + "<br/>outerWidth: " + $("#myp1").outerWidth());
					$("#myp2").html($("#myp2").html() + "<br/>outerHeight: " + $("#myp1").outerHeight());
				});
			});
		</script>
	

	
		<button>获取css属性的值</button><br>
		<p>p区域</p>
		<p></p>
	

Summary: The above is the entire content of this article, you can try it yourself and deepen Understand; I hope it will be helpful to everyone's learning. Recommended related video tutorials:

jQuery Tutorial

!

The above is the detailed content of How does jQuery operate on HTML elements and attributes? (Example of detailed code explanation). For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:博客园. If there is any infringement, please contact admin@php.cn delete
jquery实现多少秒后隐藏图片jquery实现多少秒后隐藏图片Apr 20, 2022 pm 05:33 PM

实现方法:1、用“$("img").delay(毫秒数).fadeOut()”语句,delay()设置延迟秒数;2、用“setTimeout(function(){ $("img").hide(); },毫秒值);”语句,通过定时器来延迟。

jquery怎么修改min-height样式jquery怎么修改min-height样式Apr 20, 2022 pm 12:19 PM

修改方法:1、用css()设置新样式,语法“$(元素).css("min-height","新值")”;2、用attr(),通过设置style属性来添加新样式,语法“$(元素).attr("style","min-height:新值")”。

axios与jquery的区别是什么axios与jquery的区别是什么Apr 20, 2022 pm 06:18 PM

区别:1、axios是一个异步请求框架,用于封装底层的XMLHttpRequest,而jquery是一个JavaScript库,只是顺便封装了dom操作;2、axios是基于承诺对象的,可以用承诺对象中的方法,而jquery不基于承诺对象。

jquery怎么在body中增加元素jquery怎么在body中增加元素Apr 22, 2022 am 11:13 AM

增加元素的方法:1、用append(),语法“$("body").append(新元素)”,可向body内部的末尾处增加元素;2、用prepend(),语法“$("body").prepend(新元素)”,可向body内部的开始处增加元素。

jquery中apply()方法怎么用jquery中apply()方法怎么用Apr 24, 2022 pm 05:35 PM

在jquery中,apply()方法用于改变this指向,使用另一个对象替换当前对象,是应用某一对象的一个方法,语法为“apply(thisobj,[argarray])”;参数argarray表示的是以数组的形式进行传递。

jquery怎么删除div内所有子元素jquery怎么删除div内所有子元素Apr 21, 2022 pm 07:08 PM

删除方法:1、用empty(),语法“$("div").empty();”,可删除所有子节点和内容;2、用children()和remove(),语法“$("div").children().remove();”,只删除子元素,不删除内容。

jquery怎么去掉只读属性jquery怎么去掉只读属性Apr 20, 2022 pm 07:55 PM

去掉方法:1、用“$(selector).removeAttr("readonly")”语句删除readonly属性;2、用“$(selector).attr("readonly",false)”将readonly属性的值设置为false。

jquery on()有几个参数jquery on()有几个参数Apr 21, 2022 am 11:29 AM

on()方法有4个参数:1、第一个参数不可省略,规定要从被选元素添加的一个或多个事件或命名空间;2、第二个参数可省略,规定元素的事件处理程序;3、第三个参数可省略,规定传递到函数的额外数据;4、第四个参数可省略,规定当事件发生时运行的函数。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),