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

Javascript and jQuery methods to obtain objects

不言
Release: 2018-07-17 15:29:57
Original
1957 people have browsed it

This article mainly introduces the methods of obtaining objects in Javascript and jQuery. It has certain reference value. Now I share it with you. Friends in need can refer to it

Two common methods of obtaining objects in JavaScript

Get based on the className value

document.getElementsByClassName("class value");

Get based on the ID value

document.getElementById("ID Value");

Example:

<div id="a">我是实验品</div>
<script type="text/javascript">
    var ss = document.getElementById("a");//获取ID值为a的标签并保存到ss里
    ss.style.color="red";//修改属性字体颜色为红色
</script>
Copy after login

jQuery Get Object

Get ID Object

$("#a")

Get the class object

$(".a")

Get the label object

$('input[name="c" ]')

Example

<script src="jquery-2.1.4/jquery.min.js"></script> //导入jQuery所需js包
<script type="text/javascript">
    function tj(){
        $(&#39;input[name="c"]&#39;).css("background-color","yellow");
    }
    function tj1(){
        $("#a").css("background-color","red");
    }
</script>
<input onmouseover="tj()" onmouseout="tj1()" id="a" class="b" name="c" type="submit" value="指着我" >
Copy after login

//onmouseover="" The effect displayed when the mouse is moved up

//onmouseout="" The effect displayed after the mouse is moved away

Related recommendations:

How js creates objects and their characteristics

The above is the detailed content of Javascript and jQuery methods to obtain objects. 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