How do I pass on javascript variables through a html link tag?

杀马特。学长 韩版系。学妹 提交于 2020-01-07 04:35:10

问题


<a href="javascript:size('x')">HTML Link</a>

I have the above code in my html file and want to pass on the variable 'x' to my javascript function 'size()'.

In my javascript file, I'm trying to use the variable as follows:

function size(id) 
{
   document.write("hi user number" + id);
}

I think my javascript part is correct and that my html syntax for sending the variable is wrong but having a bit of difficulty googling the correct syntax. Could someone help me here? thnx!


回答1:


I think you need a return false in there. Try rewriting the link as:

<a href="#" onclick="size( 'x' ); return false;">HTML Link</a>



回答2:


Passing JavaScript Variable to A HREF



来源:https://stackoverflow.com/questions/4392660/how-do-i-pass-on-javascript-variables-through-a-html-link-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!