如何使用 javascript/jquery 访问嵌套 html 元素的值

发布于 2025-01-12 13:46:31 字数 281 浏览 3 评论 0原文

我有这段代码:

   <td class="fieldValue " id="Contacts_detailView_fieldValue_leadsource" >
     <span class="value" data-field-type="picklist" >
      <span >Partner</span>
     </span>
   </td>

如何访问值“Partner”? 谢谢

I have this piece of code:

   <td class="fieldValue " id="Contacts_detailView_fieldValue_leadsource" >
     <span class="value" data-field-type="picklist" >
      <span >Partner</span>
     </span>
   </td>

how do i access the value "Partner"?
Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

惜醉颜 2025-01-19 13:46:31

您只需选择父元素,然后从那里转到子元素即可获取innerHtml

const value = document.querySelector(".value")
const partner = value.children; 

// partner[0] is for taking the first element 
// and then .innerHTML is for the content inside
console.log(partner[0].innerHTML)

You can simply select the parent element and from there go to the child to get the innerHtml

const value = document.querySelector(".value")
const partner = value.children; 

// partner[0] is for taking the first element 
// and then .innerHTML is for the content inside
console.log(partner[0].innerHTML)
墨离汐 2025-01-19 13:46:31

我最终采用了这个解决方案:

function textSpan(id){
    const collection = document.getElementById(id);
    const value = collection.children;
    const final = value[0].children[0].innerText;
    return final;
}console.log(textSpan('Contacts_detailView_fieldValue_leadsource'));

谢谢大家的建议!

I finally adopted this solution:

function textSpan(id){
    const collection = document.getElementById(id);
    const value = collection.children;
    const final = value[0].children[0].innerText;
    return final;
}console.log(textSpan('Contacts_detailView_fieldValue_leadsource'));

Thank you all for the suggestions!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文