简体   繁体   English

jQuery在版本1.7.1中未设置'textcontent'

[英]JQuery not setting 'textcontent' in version 1.7.1

I have the following code which is setting the target, href & textcontent for an element in HTML, this was working perfectly fine in JQuery 1.4.1 & 1.5.1 but after updating to 1.7.1 it does not change the 'textcontent' property of the visible element, the href has been updated. 我有以下代码为HTML中的元素设置目标,href和textcontent,这在JQuery 1.4.1和1.5.1中工作得很好,但是更新到1.7.1后,它不会更改'textcontent'属性可见元素的href已更新。

 $("#uriEmma").attr('textcontent', emmaUri);
 $("#uriEmma").attr('href', emmaUri);
 $("#uriEmma").attr('target', '_blank');

Any ideas why this might be? 任何想法为什么会这样?

You should use .prop() and proper casing: textContent . 您应该使用.prop()和正确的大小写: textContent Example

From documentation: 从文档:

The difference between attributes and properties can be important in specific situations. 属性和属性之间的差异在特定情况下可能很重要。 Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. 在jQuery 1.6之前,.attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。 As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. 从jQuery 1.6开始,.prop()方法提供了一种显式检索属性值的方法,而.attr()则检索属性。

.prop( propertyName )

The difference between attributes and properties can be important in specific situations. 属性和属性之间的差异在特定情况下可能很重要。 Before jQuery 1.6, the .attr() method sometimes took property values into account when retrieving some attributes, which could cause inconsistent behavior. 在jQuery 1.6之前, .attr()方法在检索某些属性时有时会考虑属性值,这可能会导致行为不一致。 As of jQuery 1.6, the .prop() method provides a way to explicitly retrieve property values, while .attr() retrieves attributes. 从jQuery 1.6开始,.prop()方法提供了一种显式检索属性值的方法,而.attr()检索属性。

For example, selectedIndex , tagName , nodeName , nodeType , ownerDocument , defaultChecked , and defaultSelected should be retrieved and set with the .prop() method. 例如,应该检索selectedIndextagNamenodeNamenodeTypeownerDocumentdefaultCheckeddefaultSelected并使用.prop()方法进行设置。 Prior to jQuery 1.6, these properties were retrievable with the .attr() method, but this was not within the scope of attr. 在jQuery 1.6之前,可以使用.attr()方法检索这些属性,但这不在attr的范围内。 These do not have corresponding attributes and are only properties. 这些没有相应的属性,而仅仅是属性。

textContent is a DOM property, and not a HTML attribute. textContent是DOM属性,而不是HTML属性。

In older versions of jQuery, the attr() function would almost always map to a property if the string case was the same as a matching property on the elements. 在旧版本的jQuery中,如果字符串大小写与元素上的匹配属性相同,则attr()函数几乎总是映射到属性。 This changed in jQuery 1.6 (as it rightly should have), so now attr() almost exclusively maps to setAttribute and the new function prop() was introduced to set properties on wrapped elements. 这在jQuery 1.6中进行了更改(这是正确的),因此现在attr()几乎唯一地映射到setAttribute并且引入了新函数prop()来设置包装元素的属性。

Aside from using prop() , you'll also need to ensure your casing is correct. 除了使用prop() ,您还需要确保大小写正确。 JavaScript and DOM properties use camel casing and start with a lower case letter. JavaScript和DOM属性使用驼峰大小写,并以小写字母开头。 So you need to use textContent , rather than textcontent . 因此,您需要使用textContent而不是textcontent

Here's a couple of ways you can set the property in jQuery 1.6+: 您可以通过以下两种方式在jQuery 1.6+中设置属性:

// Set the first element's `textContent`
$("#someEl")[0].textContent = "test";

// Set every element's textContent
$("#someEl").prop('textContent', "test");

// Set every element's text, mapping to innerText in older browsers:
$("#someEl").text("test");

nb if you want to support IE 8 and lower, you need to use text() or have a suitable test for using innerText instead of textContent . 注意,如果要支持IE 8及更低版本,则需要使用text()或进行适当的测试以使用innerText代替textContent

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM