简体   繁体   English

href链接到function js

[英]href link to function js

I want to create link that send to function:我想创建发送到 function 的链接:

    tdLink2.innerText="Delete";
    tdLink2.href="javascript:deleteDepartment(id)"

but the "id" parameters was not sent.但未发送“id”参数。 Who I can do this with the parameters?谁可以用参数做到这一点? (Javascript) Thank you. (Javascript)谢谢。

ID is not parsed in your string ID 未在您的字符串中解析

EITHER (Don't forget the extra quotes if ID is a string要么(如果 ID 是字符串,请不要忘记额外的引号

tdLink2.href="javascript:deleteDepartment('"+id+"')" 

alternative with template literals模板文字的替代方案

tdLink2.href=`javascript:deleteDepartment('${id}')`; 

I would personally keep DELETE far away from a href我个人会让 DELETE 远离 href

This is better这个更好

tdLink2.href="#"
tdLink2.addEventListener("click",function(e){
  e.preventDefault(); /stop the link
  deleteDepartment(id); // id is some global variable
})

EVEN better is to do更好的是做

tdLink2.href="#";
td.dataset.id = id; // assign to a data attribute
tdLink2.addEventListener("click",function(e){
  e.preventDefault(); // stop the link
  deleteDepartment(this.dataset.id); // pass the data attribute
})

If id is already defined variable then u can do like this如果 id 已经定义了变量,那么你可以这样做

tdLink2.href=`javascript:deleteDepartment(${id})`

U can do this if id is of type string如果 id 是字符串类型,你可以这样做

otherwise u can go for this否则你可以为此使用 go

function f(){
  deleteDepartment(id)
}
tdLink2.href='javascript:f()'

Just assign function result to property只需将 function 结果分配给属性

tdLink2.href = deleteDepartment(id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM