javascript - 对象嵌入链接内的随机变量

标签 javascript html

<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 in HTML Use DOM API to do so. Assign id attribute to the element and set data 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/

相关文章:

javascript - 如何在javascript中通过两个对象创建新对象

javascript - 刷新 Angular ui 网格数据的最佳方法。状态刷新或将新数据提供给 $scope.gridOptions.data?

javascript - D3 选择 ID parentNode

JavaScript - If 语句始终返回 true

HTML 元描述 + &

javascript - 是否可以删除我的代码中的 Ul 的子元素?

html - Bootstrap div 未按预期放置

html - 固定背景图像在 iPhone/iPad 上消失

javascript - 尝试在按钮中重新创建波纹效果动画时出现错误( Material 设计)

javascript - 预加载大面积的google map