aivnfjgj

jquery使用css类名和id获取元素

jQuery中,你可以很容易的使用css 类名和id来获得元素。

例如:

1 ID: #id

 $(‘#ida’)—选择id 为ida的所有元素,不管元素的标签名如何。

 $(‘div#ida’)—选择id为ida的所有div标签元素。

 

2 Class:.classname

$(‘.classA’)—选择类名有一个为classA的所有元素,不管其标签名如何。

$(‘div.classA’)—选择类名有一个为classa的所有div元素。

 

完整的例子如下:

<html>
<head>
<title>jQuery Get element with class name and id</title>
 
<scripttype="text/javascript"src="jquery-1.3.2.min.js"></script>
 
</head>
 
<scripttype="text/javascript">
 
$(document).ready(function(){
 
    var $element = $(\'.classABC\').html();
    alert($element);
 
    var $element = $(\'#idABC\').html();
    alert($element);
 
});
 
</script>
<body>
 
<h1>jQuery Get element with class name and id</h1>
 
        <divclass="classABC">
               This is belong to \'div class="classABC"\' 
        </div>
 
        <divid="idABC">
               This is belong to \'div id="idABC"\'
        </div>
 
</body>
</html>
效果图1:
点击确定后的效果:

分类:

技术点:

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-12-26
  • 2021-09-07
  • 2022-12-23
  • 2021-12-22
猜你喜欢
  • 2021-11-28
  • 2022-12-23
  • 2022-01-08
  • 2021-07-30
  • 2022-12-23
  • 2021-12-22
相关资源
相似解决方案