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>
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(){ $('input[name="c"]').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="指着我" >
//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!