<script>
var a=Math.floor((Math.random() * 10) + 1)
//alert(a); (For testing)
</script>
<object data='http://example.com/'+a+'.php' height='100%' type='text/html' width='100%'/>
我想像这样随机调用php的名称。谢谢。我是 html 新手。 我正在 blogger html 编辑器中尝试这个。
最佳答案
You can not concatenate
JavaScript
variables inHTML
Use DOM API to do so. Assignid
attribute to theelement
and setdata
attribute.
var a = Math.floor((Math.random() * 10) + 1);
var obj = document.getElementById('obj');
obj.setAttribute('data', 'http://example.com/' + a + '.php');
console.log(obj.outerHTML); //To demonstrate
<script src="http://gh-canon.github.io/stack-snippet-console/console.min.js"></script>
<object id='obj' height='100%' type='text/html' width='100%'></object>
关于javascript - 对象嵌入链接内的随机变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36299838/