如何将标签的自定义变量传递给javascript函数?

时间:2014-03-19 07:10:58

标签: javascript html

我有这样的HTML:

<a href="javascript:void(0);" id="13" jobtitile="UI Designer (Web Designer / Team Leader)" onclick="postResume(this.id,this.jobtitle);" class="InsideRightBoxLink">Submit Application</a>  

java脚本函数是:

function postResume(jobId,jobtitle){  
alert(jobtitle);  
//alert($("#"+jobId).attr('jobTitle'));  
}  

两者都是未定义的,请帮助我谢谢。

3 个答案:

答案 0 :(得分:1)

<script type="text/javascript">

    function postResume(obj){  
alert(obj.id);  
alert(obj.getAttribute("jobtitile"));  
}  
</script>
<a href="javascript:void(0);" id="13" jobtitile="UI Designer (Web Designer / Team Leader)" 
onclick="postResume(this);" class="InsideRightBoxLink">Submit Application</a> 

答案 1 :(得分:1)

jobtitile不是HTML有效属性,因此请使用getAttribute函数

更改

this.jobtitile

this.getAttribute("jobtitile")

试试此代码

  <a href="javascript:void(0);" id="13" 
 jobtitile="UI Designer (Web Designer / Team Leader)" 
    onclick="postResume(this.id,this.getAttribute("jobtitile"));"
 class="InsideRightBoxLink">Submit Application</a>  

答案 2 :(得分:1)

在javascript中获取属性值与jQuery不同。 使用this.getAttribue(&#39; attributeName&#39;);

<a href="javascript:void(0);" id="13" jobtitle="UI Designer (Web Designer / Team Leader)" onclick="postResume(this.id,this.getAttribute('jobtitle'));" class="InsideRightBoxLink">Submit Application</a> 


<script>
function postResume(jobId,jobtitle){  
alert(jobtitle);  
//alert($("#"+jobId).attr('jobTitle'));  
} 
</script>