jquery - 有没有任何方法可以使用 jQuery 从另一个 html 文件获取属性值?

标签 jquery

我们可以使用jQuery通过load函数获取属性值吗?

$(#id).load('url of page #source id').attr();

最佳答案

我真的不明白你在寻找什么,希望这有帮助,:D

您想在 div new-projects 中添加 ul,并且需要获取 ul 元素的属性。您可以执行以下操作

假设 HTML 是这样的

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>load demo</title>
  <style>
  body {
    font-size: 12px;
    font-family: Arial;
  }
  </style>
  <script src="https://code.jquery.com/jquery-3.4.1.js"></script>
</head>
<body>

<b>Projects:</b>
<div id="new-projects"></div>

<script>
$( "#new-projects" ).load("/load.html #projects ul", function(data){
  var x = data;
  $(x).find('ul').attr('style');  //console.log or alert this
} );
</script>

</body>
</html>

加载.html

<html>
  <body>
    <div id="projects">
      <ul style="color:blue">
        <li>Test</li>
        <li> This</li>
      </ul>
    </div>
  </body>
</html>

关于jquery - 有没有任何方法可以使用 jQuery 从另一个 html 文件获取属性值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59349621/

相关文章:

javascript - 如果用户尚未保存表单,如何测试用户是否对表单进行了任何更改

JavaScript:如何在文本字段中的值更改时显示警告框

javascript - 从过滤器中成功获取结果后如何在 Laravel 8 中的 Blade 文件中显示

javascript - 根据浏览器宽度加载图像

jquery - 将 div 的水平滚动条固定到页面底部

javascript - window.location.reload(true) 重新加载页面,但页面需要刷新才能显示更改

javascript - 将 props 传递给 React 组件中的 componentDidMount()

javascript - 遍历所有跨度并使用 JQuery 更新一个字段

javascript - 如何在javascript中处理变量

jquery - Cycle2 轮播不应更改上一个/下一个操作的事件幻灯片