【问题标题】:How can I get the id of a div inside a form using external js file如何使用外部 js 文件获取表单中 div 的 id
【发布时间】:2011-07-06 12:04:06
【问题描述】:

如何使用外部 js 文件访问或获取表单内元素的 div id?

这是一个例子 asp文件

<html>
<head>
    <script src="jquery.js"></script>
    <script src="jsqueryms.js></script>
    <script src="myjs.js"></script>
    <body onload="initmyjs();">
    <form id="formid">
        <input type="button" id="button"/>
        <div id='dialog'>this is modal dialog</dialog>
    </form>
</head>
</html>

外部js文件

function initmyjs()
{
    if document.getelementby('button')!=null
       {
                document.getelementbyid('button').onlclick=function()
                {
                      $('#dialog').dialog({ modal: true, position: [902, 345], width: 400 });
                }
       }
}

我想要的是在单击按钮后弹出一个模式对话框,如果脚本位于 asp 页面内,我可以毫无问题地访问它,但如果我将它放在外部 js 文件中,则没有任何反应,但不会显示脚本错误。我想知道脚本是否正确,除了我无法映射我的 div 的正确元素 id。提前谢谢你。

【问题讨论】:

    标签: jquery-selectors


    【解决方案1】:

    尝试将 myjs.js 的内容更改为:

    $(document).ready(function() {
        $('#button').click(function() {
            $('#dialog').dialog({ modal: true, position: [902, 345], width: 400 });
        });
    
        $('[name="linkComment"]').attr('href', '#').click(function() { 
            $("#dialog").dialog({ modal: true, position: [902, 345], width: 400 }); 
        });
    
        // Other javascript code that needs to run on page load
    });
    
    // Other javascript functions used byt he page
    

    然后去掉body标签上的onload属性。

    【讨论】:

    • 您好安德鲁,感谢您的快速回复。我使用unload属性的原因是页面需要特定的js函数并且大部分函数是javascript代码,我只想插入一个小脚本,当用户单击按钮时会调用弹出对话框/或表格内的链接标签。
    • 这是我要插入 jquery 脚本的 js 脚本部分
    • 您仍然可以通过这种方式运行其他代码。请参阅编辑后的答案。
    • 好的,我回家后会尝试,然后我会立即发布结果。感谢您的宝贵时间。
    • 'code' 链接 = hitlistDoc.getElementsByName('linkComment');链接 = hitlistDoc.getElementsByName('linkComment'); for (var i=0;i
    猜你喜欢
    • 2023-01-17
    • 2015-06-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多