Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/372.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/81.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 访问标签';使用jquery创建属性_Javascript_Jquery_Html_Dom_Attr - Fatal编程技术网

Javascript 访问标签';使用jquery创建属性

Javascript 访问标签';使用jquery创建属性,javascript,jquery,html,dom,attr,Javascript,Jquery,Html,Dom,Attr,我有一些来自服务器的数据,我在Html页面的一个Div中填充了这些数据。 我写div的方式如下: <div class="BigDiv"><label class = "AttList" Std_Id="' + Std_Id + '">' + Std_Name +'</label></div> 我想访问Std_Id属性中的Std_Id,但我不知道如何操作。。。你有什么想法吗? 谢谢 假设$(this)是对.BigDiv元素的引用: var

我有一些来自服务器的数据,我在Html页面的一个Div中填充了这些数据。 我写div的方式如下:

    <div class="BigDiv"><label class = "AttList" Std_Id="' + Std_Id + '">' + Std_Name +'</label></div>
我想访问Std_Id属性中的Std_Id,但我不知道如何操作。。。你有什么想法吗? 谢谢

假设
$(this)
是对
.BigDiv
元素的引用:

var StdName = $(this).find('label').attr('Std_Id');
var children = this.childNodes;
for (var i=0,len=children.length; i<len; i++){
    if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == 'label'){
        var StdName = this.getAttribute('Std_Id');
    }
}
或者,类似地,假设
.BigDiv
元素:

var StdName = $(this).find('label').attr('Std_Id');
var children = this.childNodes;
for (var i=0,len=children.length; i<len; i++){
    if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == 'label'){
        var StdName = this.getAttribute('Std_Id');
    }
}
var children=this.childNodes;
对于(var i=0,len=childrence.length;i假设
$(this)
是对
.BigDiv
元素的引用:

var StdName = $(this).find('label').attr('Std_Id');
var children = this.childNodes;
for (var i=0,len=children.length; i<len; i++){
    if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == 'label'){
        var StdName = this.getAttribute('Std_Id');
    }
}
或者,类似地,假设
.BigDiv
元素:

var StdName = $(this).find('label').attr('Std_Id');
var children = this.childNodes;
for (var i=0,len=children.length; i<len; i++){
    if (children[i].nodeType == 1 && children[i].tagName.toLowerCase() == 'label'){
        var StdName = this.getAttribute('Std_Id');
    }
}
var children=this.childNodes;

对于(var i=0,len=childrence.length;i您可以使用
attr
方法

var value = $('.AttList').attr('Std_Id');
编辑

好的,所以你需要为你的实现,你需要这样做

var value = $(this).find('.AttList').attr('Std_Id');

假设
this
div
或该
div

的父级,您可以使用
attr
方法

var value = $('.AttList').attr('Std_Id');
编辑

好的,所以你需要为你的实现,你需要这样做

var value = $(this).find('.AttList').attr('Std_Id');

假设
this
div
或该
div

的父级,要保存使用jQuery选择器选择的元素,请执行以下操作:

$labels = $('.BigDiv').find('label');
现在,您可以使用jQuery的foreach循环遍历每个标签:

$.each($labels, function() {
    var std_id = $(this).attr('Std_Id');
    // do something with std_id
});

要保存使用jQuery选择器选择的元素,请执行以下操作:

$labels = $('.BigDiv').find('label');
现在,您可以使用jQuery的foreach循环遍历每个标签:

$.each($labels, function() {
    var std_id = $(this).attr('Std_Id');
    // do something with std_id
});

使用
getAttribute

var labels = $(this).children('div');
var StdId = this.children[0].getAttribute("Std_Id");

请注意,根据HTML5规范,自定义属性应该以
数据-
开头,尽管大多数浏览器可以容忍它。

使用
getAttribute

var labels = $(this).children('div');
var StdId = this.children[0].getAttribute("Std_Id");

请注意,根据HTML5规范,自定义属性应该以
数据-
开始,尽管大多数浏览器都能容忍它。

我很困惑,OP想要使用jquery。这个解决方案使用了jquery,但被标记为答案??在这种情况下,您不会得到任何东西。您不需要将jquery强制到c的每一行中你写的代码。我很困惑,OP想要使用jquery。这个解决方案使用了jquery,但却被标记为答案??在这种情况下,你什么都得不到。你不需要在你写的每一行代码中强制使用jquery。我已经更新了我的答案,使之更符合你使用jquery的需要。如果你要使用jquery,那么除了替换
document.getElementById()
之外,你不能使用它,对吗?我已经更新了我的答案,使之更符合使用jQuery的需要。如果你要使用jQuery,为什么不将它用于
document.getElementById()
替换之外的其他东西,对吗?