jQuery 相当于 `return document.getElementById(theVar)'

2024-05-27

我有一个 JavaScript 函数

function $m(theVar){
    return document.getElementById(theVar)
}

上面代码的问题是,我想要传递的元素是由ajax生成的,所以我需要类似的东西.live()在 jquery 中,除非它抛出错误

如何在 jQuery 中重写上述函数,以便以后生成的任何 DOM 元素也可以被检测到。

Update:

当我的页面第一次加载时,它会加载

1) ajaxupload.js 及代码

function ajaxUpload(form,url_action,msg){
   var id_element= "pop-con";
   function $m(theVar){
        return document.getElementById(theVar)
    }
   if($m(id_element)==null){
        erro += "The element of 3rd parameter does not exists.\n";
    }
}

2)index.php和代码

<div id="popupMargin">
    <div class="close">x</div>
    <div id="pop-con"></div>
</div>
<div id="ajaxGenerateMarkUp"></div>

3)现在单击按钮,以下标记将添加到#ajaxGeneratedmarkUpdiv(通过ajax生成的标记)

<form  onSubmit="return disableForm(this);" action="crop/wizecho_upload.php" method="post" name="f" id="wizecho" enctype="multipart/form-data">
      <input id="file" type="file" name="file"  onChange="return disableForm(this), ajaxUpload(this.form,'crop/wizecho_upload.php', '&lt;br&gt;Uploading image please wait.....&lt;br&gt;'); return false;"/>
</form>

现在,在更改此输入类型文件时,在 JavaScript 中进行调用。现在它显示了错误。

[注意:我只发布了我认为可能影响我的问题的代码部分]


像这样:

return $('#' + theVar)[0];
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

jQuery 相当于 `return document.getElementById(theVar)' 的相关文章

随机推荐