Home >Web Front-end >JS Tutorial >Analysis of DOM operation examples in jQuery

Analysis of DOM operation examples in jQuery

韦小宝
韦小宝Original
2017-11-28 10:12:581600browse

The example of this article describes the DOM operation method in jQuery. The use of jQuery on dom can be said to be the greatest significance of jQuery. Let us take a look at the strength analysis of jQuery on dom operations! !

The main dom operations designed here include: Creation of dom objects (JS mode and jquery mode), modification of attributes, modification of styles, and dynamic binding of events
The code is as follows:

<html>
<head>
<script type="text/javascript" src="jquery-1.3.2.min.js"></script>
<title>动态创建对象</title>
</head>
<body>
<div id="testDiv">测试图层</div>
<img src="images/image.1.jpg" id="hibiscus" alt="Hibiscus" class="classA"/>
<input type=text id=inputTest readonly=true />
<div id="testDiv5" customer="customer data 1">获取自定义数据-1</div>
<div id="testDiv6">获取自定义数据-2</div>
<script type="text/javascript">
//document.getElementById("testDiv").innerHTML = "<div style=\"border:solid 1px #FF0000\">动态创建的div</div>";
var testDiv = document.getElementById("testDiv");
var select = document.createElement("select");
select.options[0] = new Option("加载项1", "value1");
select.options[1] = new Option("加载项2", "value2");
select.size = "2";
var object = testDiv.appendChild(select);
$("img").each(function(index) {
this.alt = "changed";//修改dom属性信息
//alert("index:" + index + ", id:" + this.id + ", alt:" + this.alt);
});
$("#inputTest").removeAttr("readonly");
//alert($("#inputTest").attr("readonly"));
$(function()
{
alert("attr(\"width\"):" + $("#testDiv").attr("width"));//undifined
alert("css(\"width\"):" + $("#testDiv").css("width"));//auto(ie6) 或1264px(ff)
alert("width():" + $("#testDiv").width()); //正确的数值1264
alert("style.width:" + $("#testDiv")[0].style.width ); //空值
})
//动态绑定单击事件
$("#testDiv5").bind("click", function(event)
{ alert($(event.target).attr("customer")); });
//绑定只执行一次的单击事件
$("#testDiv6").one("click", { customer: "customer data 2",a:"aaa" }, function(event)
{ alert(event.data.customer) });
</script>
</body>
</html>

I hope this article will be helpful to everyone’s jQuery programming. For more information, please go to PHP Chinese website to searchoh~

Related recommendations:

##JQuery uses ajax to read local json files

#JQuery image carousel effect implementation example

How to use asynchronous search jquery select plug-in

The above is the detailed content of Analysis of DOM operation examples 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