简体   繁体   English

Javascript函数可以与onclick一起使用,但不能与onfocus一起使用

[英]Javascript function works with onclick but not with onfocus

I have the following javascript function: 我具有以下javascript函数:

function makefieldlonger(element) {
    element.style.width = "550px";

}

And i call it with a RadTextBox control like this: 我用这样的RadTextBox控件调用它:

<telerik:RadTextBox ID="txtSubject" runat="server" EnableEmbeddedSkins="false"
                                    Skin="Default" Width="255px" onclick="makefieldlonger(this)"/>

This works, but i want this to happen in an onfocus event so i do the following: 这是有效的,但我想在onfocus事件中发生这种情况,所以我执行以下操作:

<telerik:RadTextBox ID="txtSubject" runat="server" EnableEmbeddedSkins="false"
                                    Skin="Default" Width="255px" onfocus="makefieldlonger(this)"/>

But it refuses to adjust its size on the page. 但是它拒绝调整页面大小。 it does call the javascript function (i tested this by putting an alert in the function and it was called). 它确实调用了javascript函数(我通过在函数中添加一个警报来测试它并且它被调用)。

So the question is: why does this function work with an onclick event but not with an onfocus event? 所以问题是:为什么这个函数适用于onclick事件但不适用于onfocus事件?

ASPX ASPX

<telerik:RadTextBox ID="txtSubject" runat="server" EnableEmbeddedSkins="false" Skin="Default" Width="255px" >
    <ClientEvents OnFocus="makefieldlonger" />
</telerik:RadTextBox>

OR You can use ClientEvents-OnFocus property to set the function as @Blade0rz comment 或可以使用ClientEvents-OnFocus属性将函数设置为@ Blade0rz注释

JavaScript JavaScript的

<script type="text/javascript">
    function makefieldlonger(sender, eventArgs)
    {
        sender._element.style.width = "550px";
    }
</script>

REF : RadControls for ASP.NET AJAX Documentation REF: ASP.NET AJAX文档的RadControls

It looks like OnFocus takes two arguments. 看起来OnFocus需要两个参数。 I suspect element in your function is referring to the event arguments. 我怀疑你的函数中的element是指事件参数。

Try this (I made it as a ClientEvent ): 试试这个(我把它作为ClientEvent ):

<telerik:RadTextBox ID="txtSubject" runat="server" EnableEmbeddedSkins="false" Skin="Default" Width="255px">
    <ClientEvents OnFocus="makefieldlonger" />
</telerik:RadTextBox>

Then your function would be: 然后你的功能是:

function makefieldlonger(sender, eventArgs) {
    sender.style.width = "550px";
}

Taken from the Telerik RadTextBox Documentation 取自Telerik RadTextBox文档

Try using OnBlur() function. 尝试使用OnBlur()函数。 You are changing size of textbox on Focus event of that textbox that might cause this error. 您正在更改该文本框的Focus事件上的文本框的大小,这可能会导致此错误。 Use Onblur so that when control leave the Textbox onblur will fire and textbox will get changed 使用Onblur,以便在控件离开文本框时触发onb​​lur并更改文本框

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

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