javascript - ( "p:first") 与 ("p").first() Jquery

标签 javascript jquery

我有一个小脚本。

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js">
</script>
<script>
$(document).ready(function(){
  $("button").click(function(){
    $( "p" ).first().replaceWith("Hello world!");
  });
});
</script>
</head>
<body>

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>
<button>Replace the first p element with new text</button>

这会将第一个

中的内容替换为“Hello world!”。

我用 2 个 jquery 脚本得到了相同的结果。

1) ( "p:first" )
2) ("p").first()

这两个脚本之间的确切区别是什么。

最佳答案

根据性能测试found here第二种方式 ($("p").first();) 速度要快得多。

关于javascript - ( "p:first") 与 ("p").first() Jquery,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20628525/

相关文章:

javascript - jQuery 在大量文本中查找/替换

javascript - 无法最大化或切换聊天框,自动滚动不起作用

javascript - $.ajax 引用错误 : data is not defined

javascript - JS 幻灯片以一种方式工作

javascript - Jquery 遍历不适用于 .closest()、.parent().parent() 等

jquery - 如何在Jquery验证规则中实现 "or"条件

javascript - 将 bool 字符串查询传递给 Django

javascript - MongoDB/EJS : How to make synchronous query and render result values in EJS

javascript - 获取面板展开后元素的更新偏移量

jquery - 菜单 <li> 悬停显示带有过渡的 div