何时在元素上使用jQuery .html或.text [英] When to use jQuery .html or .text on an element

查看:99
本文介绍了何时在元素上使用jQuery .html或.text的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题中发布(并得到答复)后,我发现了如何正确地将文本框的值分配给标签的值.答案是要么使用

In this question that I posted (and was answered), I found out how to properly assign the value of a textbox to the value of a label. The answer was to use either

$('#txtBranchName').val($('#lblBranchName').html());

$('#txtBranchName').val($('#lblBranchName').text());

一个比另一个更可取吗?是否存在性能差异,或者在特定情况下一种方法无效,而另一种无效?

Is one preferable over the other? Are there performance differences, or would one method not work but the other would in particular situations?

推荐答案

引用:.text()和.html()之间的区别,其中转义了<和>字符

实际上,两者看上去确实有些相似,但有很大不同,这取决于您的使用或意图要达到的目标,

Actually both do look somewhat similar but are quite different it depends on your usage or intention what you want to achieve ,

使用地点:

  • 使用.html()对具有html元素的容器进行操作.
  • 使用.text()修改通常具有单独的开始和结束标签的元素的文本

jQuery.html()将字符串视为HTML,jQuery.text()将内容视为文本.

jQuery.html() treats the string as HTML, jQuery.text() treats the content as text.

与.html()方法不同,.text()可以在XML和HTML中使用 文件. .text()方法的结果是一个字符串,其中包含 所有匹配元素的组合文本. (由于HTML中的变化 解析器在不同的浏览器中,返回的文本可能以换行符变化 和其他空白.)

Unlike the .html() method, .text() can be used in both XML and HTML documents. The result of the .text() method is a string containing the combined text of all matched elements. (Due to variations in the HTML parsers in different browsers, the text returned may vary in newlines and other white space.)

这篇关于何时在元素上使用jQuery .html或.text的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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