繁体   English   中英

如何在div标签内获取跨度内部文本

[英]how to get span inner text inside div tag

我在 sharepoint 页面中嵌入了 powerapps 表单。 我在 powerapps 中有一个下拉框。 在基于下拉值的 sharepoint 页面中,我想显示一些代码。 从下面的代码我想要 APAC 文本。 这里这个 html 代码是在浏览器中动态生成的。

这是我在浏览器中生成的 HTML 代码。

<div data-is-focusable="true" id="react-combobox-view-0" class="label_kohvda-o_O-label_2lsolt" tabindex="9" role="listbox" aria-expanded="false" aria-haspopup="true" aria-atomic="true" title="Region" aria-live="assertive">

<div style="width: 100%; overflow: hidden; position: relative; display: flex; height: 100%; align-items: stretch;">
<ul style="margin: 0px; padding: 5px; list-style: none; display: flex; overflow: hidden;">
<li class="selectedItem_1og5q2j">
<span class="topTagText_yz2uri-o_O-topTagText_t9v74o-o_O-topTagTextReadonly_ps5463">APAC</span></li></ul></div>

<div class="combobox-view-chevron arrowContainer_1kmq8gc-o_O-container_r2h174-o_O-containerColors_1803dea"></div></div>

使用 JavaScript,如果你有动态生成的元素类,你可以这样做: document.querySelector('.label_kohvda-o_O-label_2lsolt span.topTagText_yz2uri-o_O-topTagText_t9v74o-o_O-topTagTextReadonly_ps5463')

如果您没有元素类,根据 html 您可以执行以下操作:

 const span = document.querySelector('div div ul li span') console.log(span.innerText)
 <div data-is-focusable="true" id="react-combobox-view-0" class="label_kohvda-o_O-label_2lsolt" tabindex="9" role="listbox" aria-expanded="false" aria-haspopup="true" aria-atomic="true" title="Region" aria-live="assertive"> <div style="width: 100%; overflow: hidden; position: relative; display: flex; height: 100%; align-items: stretch;"> <ul style="margin: 0px; padding: 5px; list-style: none; display: flex; overflow: hidden;"> <li class="selectedItem_1og5q2j"> <span class="topTagText_yz2uri-o_O-topTagText_t9v74o-o_O-topTagTextReadonly_ps5463">APAC</span> </li> </ul> </div> <div class="combobox-view-chevron arrowContainer_1kmq8gc-o_O-container_r2h174-o_O-containerColors_1803dea"></div> </div>

我们可以使用 jQuery 来实现它。

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(function () {
    var region=$("div[title='Region'] li[class^='selectedItem']").text();
    alert(region);
});
</script>

或者

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
var myVar;
$(function () {         
    myVar = setInterval(getDropdownValue, 100);
});
function getDropdownValue(){
    if($("div[title='Region'] li[class^='selectedItem']").length>0){
        var region=$("div[title='Region'] li[class^='selectedItem']").text();
        alert(region);
        clearInterval(myVar);
    }   
}
</script>

暂无
暂无

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

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