jquery添加css_jQuery添加CSS,添加html元素

╰+哭是因爲堅強的太久メ 2023-02-28 08:45 135阅读 0赞

jquery添加css

jQuery add method can be used to add elements, selectors or html to the matched elements.

jQuery add方法可用于将元素,选择器或html添加到匹配的元素。

jQuery add() (**jQuery add()**)

jQuery add() method is very useful if you want to apply any jQuery function or add CSS to DOM elements in HTML.

如果您想应用任何jQuery函数或将CSS添加到HTML中的DOM元素,则jQuery add()方法非常有用。

Below example shows how we can use add() to apply css styles to ‘table’, ‘p’ and ‘li’ DOM elements in a single line. Otherwise we had to apply styles on each element one by one. It also shows how can we add HTML to any DOM element.

下面的示例显示了如何使用add()在一行中将CSS样式应用于“ table”,“ p”和“ li” DOM元素。 否则,我们必须在每个元素上一一应用样式。 它还显示了如何将HTML添加到任何DOM元素。

  1. <html>
  2. <head>
  3. <script type="text/javascript" src="jquery-1.8.3.min.js"></script>
  4. <style type="text/css">
  5. .styled{
  6. border:1px black solid;
  7. background-color: read;
  8. border-collapse: collapse;
  9. }
  10. </style>
  11. <script type="text/javascript">
  12. function styleToggle(button) {
  13. if (button.value == 'Add Style') {
  14. //using add() we can apply css to all the elements at once
  15. $('table').add('p').addClass('styled').add('li').css('background-color', 'red');
  16. button.value = 'Remove Style';
  17. } else {
  18. //using removeAttr() to remove the styling
  19. $('table').add('p').add('li').removeClass('styled').removeAttr('style');
  20. button.value = 'Add Style';
  21. }
  22. $().add("<br><strong>appending html</strong>").appendTo("p:last");
  23. }
  24. </script>
  25. <title>jQuery add() Example</title>
  26. </head>
  27. <body>
  28. <strong>Employee Table</strong>
  29. <br><br>
  30. <table>
  31. <tbody>
  32. <tr>
  33. <th>ID</th>
  34. <th>NAME</th>
  35. <th>ROLE</th>
  36. </tr>
  37. <tr>
  38. <td>1</td>
  39. <td>Pankaj</td>
  40. <td>Developer</td>
  41. </tr>
  42. <tr>
  43. <td>2</td>
  44. <td>Mike</td>
  45. <td>Manager</td>
  46. </tr>
  47. <tr>
  48. <td>3</td>
  49. <td>David</td>
  50. <td>CEO</td>
  51. </tr>
  52. <tr>
  53. <td>4</td>
  54. <td>Lisa</td>
  55. <td>Support</td>
  56. </tr>
  57. </tbody>
  58. </table>
  59. <ul>
  60. <li>list item 1</li>
  61. <li>list item 2</li>
  62. <li>list item 3</li>
  63. </ul>
  64. <p>A Paragraph</p>
  65. <p>Last Paragraph</p>
  66. <br>
  67. <input type="button" value="Add Style" onclick="styleToggle(this)"></input>
  68. </body>
  69. </html>

jQuery添加CSS –自己尝试 (**jQuery add css – Try it Yourself**)

Here are the screenshots of HTML page when we click on the button multiple times.

这是当我们多次单击按钮时HTML页面的屏幕截图。

After clicking on “Add Style” button for first time.

首次单击“添加样式”按钮后。

After clicking on “Remove Style” button for first time.

第一次单击“删除样式”按钮后。

翻译自: https://www.journaldev.com/1028/jquery-add-css-add-html-element

jquery添加css

发表评论

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

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

相关阅读

    相关 jQuery添加元素

    [jquery如何在元素后面添加一个元素][jquery] [jQuery][]添加插入元素技巧: jquery添加分为在指定元素的里面添加和外面添加两种: 里面添加使用

    相关 JQuery添加删除元素

    JQuery提供了添加元素的四种方法: append() - 在被选元素内部的结尾插入指定内容 prepend() - 在被选元素内部的开头插入指定内容 after()