通过Javascript设置事件 [英] Setting event via Javascript

查看:63
本文介绍了通过Javascript设置事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试通过JavaScript设置元素的点击但遇到一些麻烦。加载aspx网页时将设置该事件。但是,它在页面加载时执行事件,而不是在实际单击元素时执行。最后,在加载页面后,click事件不起作用。我已经尝试使用addEventListener和click方法设置事件,但无济于事。以下是代码示例:



Attempting to set the click for an element via JavaScript but having some trouble. The event is to be set when an aspx web page loads. However, it's executing the event on page load rather than when the element is actually clicked. Lastly, the click event is not working after the page is loaded. I've tried setting the event using both the addEventListener and click methods but to no avail. Here's a sample of the code:

<a class="HelpContext" title="Help" id="NonModalHelpContext" style="removed30px;">?</a>







ScriptManager.RegisterStartupScript(Me.Page, Me.GetType(), "SetHelpContext", "setHelpContext('" & sPid & "');", True)







<script language="javascript" type="text/javascript">


    function setDefaultInvoice(aspCheckBoxID) {
        
        oRegExp = new RegExp(aspCheckBoxID)                        
        //window.alert(aspCheckBoxID)

        for (i = 0; i < document.forms[0].elements.length; i++) 
        {
            elm = document.forms[0].elements[i]
            if (elm.type == 'checkbox' && elm.id.indexOf('grdInvoiceTemplates') > -1) 
            {
                //window.alert(elm.id)
               // window.alert(elm.name)
                                
                if (oRegExp.test(elm.id)) {                
                    elm.checked = true;
                }
                else {
                    elm.checked = false;
                }    
            }
        }
    }


// Moved below
//    function setHelpContext(sUrl) {
//        var ctrl = window.parent.document.getElementById('NonModalHelpContext');
//        ctrl.addEventListener("click", window.open("HelpPage.aspx?" + sUrl, "HelpPage", "width=500,height=300,toolbar=no,scrollbars=yes,resizable=no,dependent=yes", false), false);
//    }
</script>


<html xmlns="http://www.w3.org/1999/xhtml" >
......

</html>


<script language="javascript" >
    // Get a reference to the PageRequestManager.
    var prm = Sys.WebForms.PageRequestManager.getInstance();

    // Using that prm reference, hook _initializeRequest
    // and _endRequest, to run our code at the begin and end
    // of any async postbacks that occur.
    prm.add_initializeRequest(InitializeRequest);
    prm.add_endRequest(EndRequest);

    // Executed anytime an async postback occurs.
    function InitializeRequest(sender, args) {
        // Change the Container div's CSS class to .Progress.
        $get('InvoiceManagerContainer').className = 'Progress';

        // Get a reference to the element that raised the postback,
        //   and disables it.
        $get(args._postBackElement.id).disabled = true;
    }

    // Executed when the async postback completes.
    function EndRequest(sender, args) {
        if (sender._postBackSettings.sourceElement.id !== '') {
            // Change the Container div's class back to .Normal.
            $get('InvoiceManagerContainer').className = 'Normal';
            // Get a reference to the element that raised the postback
            //   which is completing, and enable it.
            $get(sender._postBackSettings.sourceElement.id).disabled = false;
        }
    }

function setHelpContext(sUrl) {
        var ctrl = window.parent.document.getElementById('NonModalHelpContext');
        ctrl.addEventListener("click", window.open("HelpPage.aspx?" + sUrl, "HelpPage", "width=500,height=300,toolbar=no,scrollbars=yes,resizable=no,dependent=yes", false), false);
    }

         
</script>





感谢任何帮助。



Any help is appreciated.

推荐答案

get(' InvoiceManagerContainer')。className = ' Progress';

// 获取引发回发的元素的引用,
// 并禁用它。
get('InvoiceManagerContainer').className = 'Progress'; // Get a reference to the element that raised the postback, // and disables it.


get( args._postBackElement.id).disabled = true ;
}

// 异步回发完成时执行。
function EndRequest(sender,args){
if (sender._postBackSettings。 sourceElement.id!== ' '){
// 将Container div的类更改回.Normal。
get(args._postBackElement.id).disabled = true; } // Executed when the async postback completes. function EndRequest(sender, args) { if (sender._postBackSettings.sourceElement.id !== '') { // Change the Container div's class back to .Normal.


get(' InvoiceManagerContainer')。className = ' 正常;
// 获取引发回发的元素的引用
// 正在填写并启用它。
get('InvoiceManagerContainer').className = 'Normal'; // Get a reference to the element that raised the postback // which is completing, and enable it.


这篇关于通过Javascript设置事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆