JavaScript变量等于jQuery选择器创建开始和结束标记。为什么? [英] JavaScript variable equals jQuery selector creates open and closing tags. Why?

查看:99
本文介绍了JavaScript变量等于jQuery选择器创建开始和结束标记。为什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请你好。我的第一个问题。我正在学习JavaScript和jQuery。谷歌没有多大帮助,因为我不知道如何提出正确的问题。需要人为干预。我想弄清楚这个简单的代码是怎么回事:

Please be nice. My first question here. I'm learning JavaScript and jQuery. Google isn't much help because I don't know how to ask the right question. Need human intervention please. I'm trying to figure out what is going on with this simple bit of code:

var myVar = $(< p> );

这会创建一个开始和结束< p> 标签和我不明白为什么。

This creates an opening and closing <p> tag and I don't understand why.

接下来,我将此段落添加到现有元素 #myDiv 。例如:

Next, I'll add this paragraph to an existing element #myDiv. For example:

$(myDiv)。html(myVar); 会产生以下结果:

< div id =myDiv>< p>< / p>< / div>

继续......

$(myDiv)。html(myVar .text(段落的字符串));

结果:

< div id =myDiv>< p>段落的字符串< / p>< / div>

为什么第一个代码段会创建一个开始和结束< p> 标记?这叫做什么?

Why does that first snippet create an opening and closing <p> tag? What is this called?

推荐答案

在纯JavaScript中,这只是一个更简洁的方法:

It's simply a more concise method of this in pure JavaScript:

var myVar = document.createElement("p");

这就是这样的jQuery:

And that goes to jQuery like this:

var myVar = $("<p></p>");

因为它是jQuery,它变得越来越简洁,最终变成:

And because it's jQuery, and it gets more and more concise, it eventually becomes:

var myVar = $("<p>");

这篇关于JavaScript变量等于jQuery选择器创建开始和结束标记。为什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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