首页 > 解决方案 > 如何在div标签内获取跨度内部文本

问题描述

我在共享点页面中嵌入了 powerapps 表单。我在 powerapps 中有一个下拉框。在基于下拉值的共享点页面中,我想显示一些代码。从下面的代码我想要 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>

标签: javascriptsharepointpowerapps

解决方案


我们可以使用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>

推荐阅读