使用js动态设置href属性

时间:2019-03-31 14:39:02

标签: javascript

我能够像这样正确设置href:

var a = document.createElement("a");
a.setAttribute('href','doWork()');

但是,当函数需要参数时,我不知道如何执行此操作:

var a = document.createElement("a");
a.setAttribute('href','doWork(money)';

#Using it like this, the url is doWork(money), but I need doWork(1), doWork(2)...
#I used a for loop, but doWork(i), when used with '' gives 'doWork(i)'.

#I have function doWork(money), with values of money ranging from 1 to 10.

我该怎么办?

我已经按照链接尝试过...我的重复了。

for(var i=1; i<=10;i++){

 var link = 'doWork(i)'   #Sets link = doWork(i)
 var link = doWork(i);    #Nothing happens here

 a.setAttribute('href','doWork(i)';

1 个答案:

答案 0 :(得分:-1)

尝试这样。

var a = document.createElement("a");
a.setAttribute('href',`doWork(${money})`);