jQuery 获取属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4792196/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 17:56:56  来源:igfitidea点击:

jQuery getAttribute

jquery

提问by theHack

i have an "uid" attribute and it's value on my "body" tag...

我有一个“uid”属性,它在我的“body”标签上的值...

$(document).ready(regFunct());

function regFunct(){
   alert($("body").attr("uid"));
}

but it says "undefined". something wrong?

但它说“未定义”。有事吗?

回答by TheVillageIdiot

try:

尝试:

$(document).ready(function(){ 
     regFunct();
 });

function regFunct(){
     alert($("body").attr("uid"));
 }

回答by jAndy

You are putting the return-value from regFunctinto the .ready()handler method. That is, undefinedbecause your function does not return anything.

您正在将返回值regFunct放入.ready()处理程序方法中。也就是说,undefined因为您的函数不返回任何内容。

You need to pass in the function reference, by omitting the parenthesis:

您需要通过省略括号来传递函数引用:

$(document).ready(regFunct);

回答by Baptiste Pernet

Hi adding custom attribute is a bad behavior. have a look at http://www.w3schools.com/tags/tag_body.aspyou will no uid attribute.

嗨,添加自定义属性是一种不好的行为。看看http://www.w3schools.com/tags/tag_body.asp你不会有 uid 属性。

If you want to store you own data. use $.data(see documentation in jQuery site)

如果你想存储你自己的数据。使用$.data(请参阅jQuery 站点中的文档)

// save the value
$('body').data('uid', mycustomvalue);
// retrieve the value
$('body').data('uid');

If the data come from the server, you can still do

如果数据来自服务器,你仍然可以做

<script type="text/javascript">
   $(function() {
      $('body').data('uid', '<?php echo $_GET[uid] ?>');
   });
</script>

回答by Harish

$(document).ready(regFunct);

set to rel tag and check!

设置为 rel 标签并检查!

function regFunct(){
   alert($("body").attr("rel"));
}

回答by raRaRa

UPDATE: It seems to be undefined in Internet Explorer since uid is not a known attribute. The fix would be to use the jQuery.data()to insert data to an element.

更新:它在 Internet Explorer 中似乎未定义,因为 uid 不是已知属性。解决方法是使用jQuery.data()将数据插入元素。

$(document).ready(regFunct());

function regFunct(){
    $('body').data('uid', 1337);
    alert($('body').data('uid'));
}

But you would have to assign the uid yourself. For example you could do it via <script>on the page.

但是您必须自己分配 uid。例如,您可以通过<script>在页面上进行操作。

<script type="text/javascript">
    $(document).ready(setData());

    function setData(){
        $('body').data('uid', 1337);
    }
</script>

More on jQuery.data() on http://api.jquery.com/jQuery.data/

有关 jQuery.data() 的更多信息,请访问http://api.jquery.com/jQuery.data/

Working example: http://jsfiddle.net/6Bwtt/1/

工作示例:http: //jsfiddle.net/6Bwtt/1/

BUT if you are trying to save data on the page from the server-side, you could just have some invisible element after the body, for example <div id="uid" style="display:none;">Your ID here</div>then fetch it using $("#uid").html();Voila! :)

但是,如果您尝试从服务器端保存页面上的数据,则您可以在正文之后添加一些不可见的元素,例如,<div id="uid" style="display:none;">Your ID here</div>然后使用$("#uid").html();Voila获取它!:)