欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

jQuery中DOM操作实例分析_jquery

程序员文章站 2022-04-24 08:21:23
...
本文实例讲述了jQuery中DOM操作的方法。分享给大家供大家参考。具体分析如下:

这里主要设计的dom操作包括:dom对象的创建(JS方式和jquery方式)、属性的修改、样式的修改、动态绑定事件
代码如下:

复制代码 代码如下:
http://www.w3.org/1999/xhtml">


动态创建对象


测试图层

jQuery中DOM操作实例分析_jquery

获取自定义数据-1

获取自定义数据-2

";
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) });


相关标签: jQuery DOM 操作