Home > Web Front-end > JS Tutorial > body text

Summary of usage examples of DOM elements and attributes in jquery

伊谢尔伦
Release: 2017-06-19 10:52:29
Original
1057 people have browsed it

DOM element methods

1. .get()

$(selector).get(index)//可选。规定获取哪个匹配元素(通过 index 编号)。
//get() 方法获得由选择器指定的 DOM 元素
$("button").click(function(){
  x=$("p").get(0);
  $("div").text(x.nodeName + ": " + x.innerHTML);
});//获得第一个 p 元素的名称和值
Copy after login

2. .index()

$(selector).index()//获得第一个匹配元素相对于其同胞元素的 index 位置,如果未找到元素,index() 将返回 -1。
$(selector).index(element)//获得元素相对于选择器的 index 位置
$("li").click(function(){
  alert($(this).index());
});//获得第一个 p 元素的名称和值
Copy after login

3. .size()

$(selector).size()//返回被 jQuery 选择器匹配的元素的数量
Copy after login

4. .toArray()

$(selector).toArray()//以数组的形式返回 jQuery 选择器匹配的元素
$("button").click(function(){
  x=$("li").toArray()
  for (i=0;i<x.length;i++)
    {
    alert(x[i].innerHTML);
    }
});//将 li 元素转换为数组,然后输出该数组元素的 innerHTML
Copy after login

Attributes

1. jquery

The string returned by the jquery attributecontains the version number of jquery

$().jquery
$("button").on("click",function(){ 
  var version = $().jquery;
  alert("You are running jQuery version: " + version);
});
Copy after login

2. jQuery.fx.interval

jQuery.fx.interval = milliseconds;

jQuery.fx.interval property is used to change the animation running rate in milliseconds. This property can be manipulated to adjust the number of frames per second at which the animation runs.

Specifies the animation running rate in milliseconds. The default is 13 milliseconds

$("#toggle").on("click",function(){
  $("div").toggle(5000);
});
$("#interval").on("click",function(){
  jQuery.fx.interval = 500;
});//以较少的帧数来运行 <div> 元素的动画
Copy after login

3. jQuery.fx.off

jQuery.fx.off property is used to globally disable or enable all animations

jQuery.fx. off = true|false;

The default value is false, which allows the animation to run normally. When set to true, all animation methods will be disabled, which will set the element to its final state instead of displaying the effect

$("#true").click(function(){
  jQuery.fx.off = true;
});
$("#false").click(function(){
  jQuery.fx.off = false;
});
$("#toggle").click(function(){
  $("div").toggle("slow");
});
Copy after login

4. jQuery.support

jQuery.support.propvalue

The jQuery.support property contains a set of properties that represent different browser features or vulnerabilities.

This property is mainly used internally by jQuery

$(document).ready(function(){
  $("p").html("This browser can create XMLHttpRequest object: " + jQuery.support.ajax);
});测试浏览器是否能创建 XMLHttpRequest 对象
Copy after login

5. length

The length property contains the number of elements in the jQuery object

$(selector).length
$("button").click(function(){
  alert($("li").length);
});//输出 <li> 元素的数目
Copy after login

The above is the detailed content of Summary of usage examples of DOM elements and attributes in jquery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template