简体   繁体   English

使用 Jquery 获取 data-fieldname 属性的子/子元素的值

[英]Fetch the value of child/sub element of the data-fieldname attribute using Jquery

I am trying to fetch the value of the child element of the html data attribute.我正在尝试获取 html 数据属性的子元素的值。 It returns me object when i call in console当我在控制台中调用时它返回我对象

$("td").find("[data-fieldname='name']").children("span")

but i am not able to fetch the value and it returns undefined when i call但我无法获取该值,并且在我调用时返回 undefined

$("td").find("[data-fieldname='name']").children("span").val()

Can someone suggest me where I am doing wrong?有人可以建议我哪里做错了吗? I have no other distinct way, to select the required span.我没有其他不同的方法来选择所需的跨度。

Case Html案例 Html

<pre>
    <td class="listViewEntryValue medium vte-hover-edit-container" data-field-type="string" nowrap="" data-fieldname="name" data-columnname="name" style="position: relative;">
        <span class="vte-display-value dm-test">Mariana Scavelli</span>
        <span class="vte-edit-show-btn cursorPointer" style="position: absolute; right: 0px; display: none;">&nbsp;<i title="Edit" class="icon-pencil"></i></span>
    </td>
</pre>
$("td").find("[data-fieldname='name']").children("span").val()

to loop trough all span child elements and get the text inside the span:循环遍历所有跨度子元素并获取跨度内的文本:

$("td").find("[data-fieldname='name']").children("span").each(function(){
    let text = $(this).text();
});

to loop trough all span child elements and get the name of the element use:循环遍历所有 span 子元素并获取元素的名称,请使用:

$("td").find("[data-fieldname='name']").children("span").each(function(){
    let name = $(this).attr('name');
});

span is not an input element and doesn't have a value. span 不是输入元素,也没有值。 .text() gets the text inside the span element. .text()获取 span 元素内的文本。 Span supports only HTML Global Attributes. Span 仅支持 HTML 全局属性。 More information about these attributes can be read here.可以在此处阅读有关这些属性的更多信息。

https://www.w3schools.com/tags/ref_standardattributes.asp https://www.w3schools.com/tags/ref_standardattributes.asp

As you can see name is not listed here.如您所见,此处未列出名称。 Therefore name is not supported on span elements in both the html 4.01 and 5 standards.因此,名称在 html 4.01 和 5 标准中都不支持 span 元素。 Most likely it will work in most browsers but some browsers that are more strict on the standard might cause issues with your code.它很可能适用于大多数浏览器,但一些对标准更严格的浏览器可能会导致您的代码出现问题。

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

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