我有一个onclick
加载iframe的事件。我需要能够动态更改onclick
偶数事件中URL的一部分。最好的方法是什么?我需要动态替换的部分在下面以“此处为变量***”突出显示。
var ****variable here*** = '343h343432e';
<button type="button" id="load_2" class="btn btn-info" onClick="document.getElementById('attri_map').height = '520';document.getElementById('attri_map').src='https://example.com/****variable here***';document.getElementById('load_2').style.display = 'none';">Click to Load</button>
<iframe id='attri_map' width='100%' height="0" frameborder='0' allowfullscreen webkitallowfullscreen mozallowfullscreen oallowfullscreen msallowfullscreen></iframe>
对于您来说,这是提取JavaScript的好机会,因为内联并不是真正的好习惯。
删除onClick
HTML中元素上的事件,然后可以执行以下操作:
(我还放入attri_map
了一个变量,以消除重新查询同一元素的DOM的开销。)
var variable = "example";
document.getElementById("load_2").onclick = function () {
var attrMap = document.getElementById('attri_map');
attrMap.height = '520';
attrMap.src='https://example.com/' + variable;
document.getElementById('load_2').style.display = 'none';
};
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句