jQuery的1.3 - 问题与设置属性值 [英] jQuery 1.3 - Issue with Setting an Attribute Value

查看:90
本文介绍了jQuery的1.3 - 问题与设置属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,计算器,所以尝试是好的。 ;-D

This is my first stackoverflow question, so try to be nice. ;-D

我的问题是这样的,我重构了一些现有的JavaScript code和使用jQuery做。在几个地方我遇到类似下面的JavaScript code:

My issue is this, I am refactoring some existing javascript code and using jQuery to do it. In several places I've come across javascript code similar to the following:

// some js code working with the customAttribute value
javascriptElementObject.customAttribue = void(0);

该javascriptElementObject现在是一个jQuery对象,我一直在尝试使用以下code做同样的事情:

The javascriptElementObject is now a jQuery object and I have been attempting to use the following code to do the same thing:

// some js code working with the customAttribute value
javascriptElementObject.attr("customAttribute", void(0));

不过,这似乎并不能做任何事情。下面code然而作品:

However, this does not seem to be doing anything. The following code works however:

javascriptElementObject.get(0).customAttribute = void(0);

我知道jQuery的removeAttr()函数,但没有用它,到目前为止,因为我不知道这是否等同于设置属性值无效(0)。

I'm aware of jQuery's removeAttr() function, but have not used it so far because I don't know if it's equivalent to setting the attribute value to void(0).

所以我想这真的意味着我有2个问题:

So I guess that really means I have 2 questions:


  1. 为什么不第一jQuery的版本工作?

  2. 的获得(0)= .customAttribue无效(0);和.removeAttr(customAttribute);相当于

感谢。

推荐答案

jQuery的喜欢重载它的方法这样:

jQuery likes to overload its methods so:

obj.attr( name ) //retrieves the attribute value
obj.attr( name, value ) //sets the attribute

obj.attr( name, void(0) ) == obj.attr( name, null ) == obj.attr( name ) //i.e retrieving the attribute

您可能想尝试以下,如果你想设置一个空属性。

You might want to try the following if you want to set an empty attribute

obj.attr( name, '' )

这也将适用于其他方法 jQuery.html()例如:

This will also apply to other methods jQuery.html() for example

这篇关于jQuery的1.3 - 问题与设置属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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