JQuery获取设置内容及属性

野性酷女 2021-09-14 02:44 515阅读 0赞

JQuery捕获内容和属性

①获取文本内容——text()、html()、val()

text():设置或返回所选元素的文本内容

html():设置或返回所选元素的内容(包括html标记)

val():设置或返回表单字段的值

实例:

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=0">
  6. <title>get Text</title>
  7. <link rel="stylesheet" href="">
  8. <style>
  9. div{
  10. color: #aa3a4f;
  11. font-size: 23px;
  12. }
  13. </style>
  14. <script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
  15. <script>
  16. $(function () {
  17. var text=$('div').text();//获取所选元素文本值
  18. $('div').text('this is a new text'); //设置所选元素的文本值
  19. alert('The Text is: '+text); //弹出原文本值
  20. var text2=$('div').text()
  21. alert('The New Text is: '+text2) //弹出新文本值
  22. var text=$('div').html(); //获取所选元素文本值
  23. alert('The Text is: '+text);
  24. $('div').html('<i>this is a new text</i>'); //重新设置所选元素的内容,并倾斜文本
  25. var text2=$('div').html();
  26. alert('The New Text is: '+text2) //html()会返回html标记
  27. var text=$('input').val(); //获取所选表单字段值
  28. alert('The Value is: '+text+' !');
  29. $('input').val('Hello new World'); //设置所选表单字段值
  30. var text2=$('input').val();
  31. alert('The New Value is: '+text2+' !')
  32. })
  33. </script>
  34. </head>
  35. <body>
  36. <div>this is a text</div>
  37. <input type="text" value="Hello World">
  38. </body>
  39. </html>

标注:以上方法设置所选元素内容时,括号里面是可以设置回调函数的,不单是字符串!

②获取属性

attr():该方法用来获取所选元素的属性值

实例:

  1. <script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
  2. <script>
  3. $(function () {
  4. var href=$('a').attr('href');
  5. alert(href);
  6. })
  7. </script>
  8. </head>
  9. <body>
  10. <a href="http://www.baidu.com/">This is a Link</a>
  11. </body>

同样的,attr()方法也允许设置属性值,也允许同时设置多个属性值,例如:

  1. <script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>
  2. <script>
  3. $(function () {
  4. $('a').attr({ //设置多个属性值的时候,要用{}括起来,以键值对的形式书写,各属性之间用“,”隔开
  5. 'href':'http://blog.csdn.net/',
  6. 'title':'the new title'
  7. });
  8. var href=$('a').attr('href');
  9. alert(href);
  10. })
  11. </script>
  12. </head>
  13. <body>
  14. <a href="http://www.baidu.com/" title="click me">This is a Link</a>
  15. </body>

attr()同样支持回调函数,回调函数由两个参数:被选元素列表中当前元素的下标,以及原始(旧的)值。然后以函数新值返回你希望使用的字符串

实例:

  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Attr()</title>
  6. <script src="//libs.baidu.com/jquery/1.10.2/jquery.min.js">
  7. </script>
  8. <script>
  9. $(document).ready(function(){
  10. $("button").click(function(){
  11. $("#school").attr("href", function(i, origValue){
  12. return origValue + "/jquery";
  13. });
  14. });
  15. });
  16. </script>
  17. </head>
  18. <body>
  19. <p><a href="http://blog.csdn.net/" id="school">CSDN论坛</a></p>
  20. <button>修改 href值</button>
  21. <p>点击按钮修改后,可以点击链接查看 href 属性是否变化。</p>
  22. </body>
  23. </html>

发表评论

表情:
评论列表 (有 0 条评论,515人围观)

还没有评论,来说两句吧...

相关阅读