我试图使用JavaScript随机函数为不同的值传递给href
属性 这是我的代码,但它不是jQuery的部分是不工作:如何使用JavaScript或jQuery将变量值传递给href属性?
的jQuery: -
var numbers = ["22332233", "44455566", "12322122", "44455566", "12322122", "44455566", "12322122"];
var result = Math.floor(Math.random() * 5);
console.log(result);
console.log(numbers[result]);
$("#phone-tel").attr("href", numbers[result]);
HTML: -
<a id="phone-tel" href="">+353123456789</a>
当前有什么问题? – guradio
我会首先确认jquery是在页面完全加载完成后执行的,并且html完全构建,让你的jquery在$(function(){})中; – Isac