"this"的工作javascript中的指针 [英] Working of "this" pointer in javascript
本文介绍了"this"的工作javascript中的指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用三个"LinkButtons"和一个隐藏字段".当用户单击LinkButton时,应该将Hidden值更改为LinkButton Text.因此,我不是使用3个不同的OnClientClick函数,而是使用单个工作函数.
为了完成我的任务,我需要在javascript中使用"this"指针.
我在work()函数中使用了"this"指针.
I am using three "LinkButtons" and a "Hidden Field". When user click on LinkButton, The Hidden value should be changed to LinkButton Text.So instead of using 3 different functions for OnClientClick, I am using a single work Function.
To achive my task I need to use "this" pointer in javascript.
I used "this" pointer in work() Function.
<asp:LinkButton ID="lnkbProjects" runat="server"
OnClientClick="work();">Home</asp:LinkButton>
<asp:LinkButton ID="lnkbDocuments" runat="server"
OnClientClick="work();">Section</asp:LinkButton>
<asp:LinkButton ID="lnkbItems" runat="server"
OnClientClick="work();" >Class</asp:LinkButton>
<asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />
<script type="text/javascript">
function work()
{
document.getElementById('<%=hidlabel.ClientID%>').value=
document.getElementById('<%=this.ClientID%>').innerText;
}
</script>
尝试此操作时,出现运行时错误
"Microsoft JScript运行时错误:需要对象"
When I tryed this, I am getting runtime error
"Microsoft JScript runtime error: Object required"
推荐答案
http: //www.quirksmode.org/js/this.html [ ^ ]可以为您提供帮助.
http://www.quirksmode.org/js/this.html[^] could help you.
感谢Q,这就是tha ans
Thanks for the Q and here is tha ans
<asp:LinkButton ID="lnkbProjects" runat="server"
OnClientClick="work(this);">Home</asp:LinkButton>
<asp:LinkButton ID="lnkbDocuments" runat="server"
OnClientClick="work(this);">Section</asp:LinkButton>
<asp:LinkButton ID="lnkbItems" runat="server"
OnClientClick="work(this);" >Class</asp:LinkButton>
<asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />
<script type="text/javascript">
function work(getval)
{
document.getElementById('hidlabel').value=
document.getElementById(getval.id).innerText;
}
</script>
另一种使用方式:
another way of using this:
<asp:LinkButton ID="lnkbProjects" runat="server">Home</asp:LinkButton>
<asp:HiddenField ID="hidlabel" runat="server" Value="I am old value" />
<script type="text/javascript">
document.getElementById('<%=lnkbProjects.ClientID%>').onclick = work;
function work()
{
document.getElementById('<%=hidlabel.ClientID%>').value=
document.getElementById(this.id).innerText;
}
</script>
这篇关于"this"的工作javascript中的指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文