jQuery $(& lt; p& gt;')和$(' p')有什么区别 [英] jQuery what is the difference between $('<p>') and $('p')

查看:106
本文介绍了jQuery $(& lt; p& gt;')和$(' p')有什么区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释jQuery中$('<p>')$('p')之间的区别.

Can someone explain to me the difference between $('<p>') and $('p') in jQuery.

例如,如果我写:

$('body').append($('<p>').html('hello my friend'));

然后出现文本你好,我的朋友".

then the text 'hello my friend' appears.

但是,当我写的时候:

$('body').append($('p').html('hello my friend'));

那什么也没发生.

推荐答案

区别:

$('<p>')创建一个新的段落元素

$('<p>') creates a new paragraph-element

$('p')选择DOM中的所有段落元素

$('p') select all paragraph-elements in the DOM

您的案件:

示例1:

$('body').append($('<p>').html('hello my friend'));

将创建一个新的段落元素,为其提供一些文本,然后将该元素添加到正文中.

Will create a new paragraph-element, give it some text, and the element will then be added to the body.

示例2:

$('body').append($('p').html('hello my friend'));

将选择DOM中已经存在的所有段落元素并将其内容更改为你好,我的朋友",然后将它们全部附加到正文中,这没有多大意义,因为它们已经存在于DOM中

Will select all paragraph-elements already present in the DOM and change their content to 'hello my friend', it will then append them all to the body, which doesn't make much sense, since they already are in the DOM.

由于在运行此代码时看不到任何内容,因此页面可能不包含任何<p>元素,因此没有任何内容与$("p")选择器匹配.

Since nothing gets visible when you run this code, your page likely doesn't contain any <p> elements, so nothing matches the $("p") selector.

这篇关于jQuery $(&amp; lt; p&amp; gt;&amp;#39;)和$(&amp;#39; p&amp;#39;)有什么区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆