为getelementbyid动态生成的HTML获取null



am在sharepoint页面中嵌入powerapps表单。我在powerapps中有一个下拉框。在sharepoint页面中基于下拉值,我想显示一些代码。从下面的代码我想要APAC文本。我这里没有包含divchatbutton 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代码

<script>
function runAfterEverythingElse(){   
var nav = document.getElementById("divChatButton");
nav.style.display='none';
GetRegion();
}
_spBodyOnLoadFunctionNames.push("GetRegion");`
function GetRegion(){
// do whatever you like here
var myDiv = document.getElementById('react-combobox-view-0');
var x = myDiv.getElementsByTagName('div');
var y = x[0].getElementsByTagName('ul');
var z = y[0].children[0].innerText;
if(z=="AMER")
{
var nav = document.getElementById("divChatButton");
nav.style.display='block';
}
else
{
var nav = document.getElementById("divChatButton");
nav.style.display='none';
}
setTimeout(yourFunction, 2000);
}
</script>
下面的jQuery代码供您参考。
<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();
if(region=="AMER"){
$("#divChatButton").show();
}else{
$("#divChatButton").hide();
}
clearInterval(myVar);
}   
}
</script>

相关内容

  • 没有找到相关文章

最新更新