Javascript 将变量指定给div标记中的属性
我有以下代码 我的要求是获取存储在Javascript 将变量指定给div标记中的属性,javascript,html,variables,attributes,Javascript,Html,Variables,Attributes,我有以下代码 我的要求是获取存储在myValue变量中的路径,并将其分配给div标记中的w3 include html属性 但是,我无法将myValue中显示的路径分配给div标记中的w3 include html属性 我尝试了中介绍的解决方案。此链接讨论img标签。我尝试了div标签的相同解决方案,但无法解决我的问题 如有任何建议,将不胜感激 <script> var myValue = "Tables/"+"FileName"+".html"; </script&g
myValue
变量中的路径,并将其分配给div
标记中的w3 include html
属性
但是,我无法将myValue中显示的路径分配给div
标记中的w3 include html
属性
我尝试了中介绍的解决方案。此链接讨论img标签。我尝试了div标签的相同解决方案,但无法解决我的问题
如有任何建议,将不胜感激
<script>
var myValue = "Tables/"+"FileName"+".html";
</script>
<div id="openModal" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<div w3-include-html=myValue></div>
</div>
</div>
var myValue=“Tables/”+“FileName”+“.html”;
我认为您可以添加一个间隔和检查值,它将自动检查更新。一些代码示例:
var myValue=“Tables/”+“FileName”+“.html”,element=document.querySelector('div[w3 include html]”);
函数checkUpdate(){
if(myValue!==element.getAttribute('w3-include-html')){
element.setAttribute('w3-include-html',myValue)
console.log('更新为%s',myValue);
}
};
设置间隔(检查更新,100);
setTimeout(()=>myValue=“Test”,200);//仅用于更新示例。
您可以创建一个函数,该函数在任何时候启动时将值(通过)分配给div:
var myValue='Tables/'+'FileName'+'.html';
var div=document.querySelector('div[w3 include html]');
log('函数调用前的值:'+div.getAttribute('w3-include-html');
insertMyValue();
log('函数调用后的值:'+div.getAttribute('w3-include-html');
函数insertMyValue(){
div.setAttribute('w3-include-html',myValue);
}
我通过添加id解决了这个问题,如下所示:
<div id="openModal" class="modalDialog">
<div>
<a href="#close" title="Close" class="close">X</a>
<div id="schema"></div>
</div>
</div>
<script>
var myValue = "Tables/"+"Thing"+".html";
document.getElementById("schema").setAttribute('w3-include-html',myValue);
</script>
var myValue=“Tables/”+“Thing”+“.html”;
document.getElementById(“schema”).setAttribute('w3-include-html',myValue);
是否可以为所涉及的div分配一个类或id?您的html语法有点不正确<代码>myValue应该用引号括起来。并不是说这是问题的根源,而是一件很好的事情。请确保您的浏览器支持这一点tag@JeroenHeier分配id帮助我解决了这个问题。谢谢