相当于`return document.getElementById(theVar)'的jQuery [英] jQuery equivalent to `return document.getElementById(theVar)'
问题描述
我有一个javascript函数
I've a javascript function
function $m(theVar){
return document.getElementById(theVar)
}
上述代码的问题是,我要传递的元素是由ajax生成的,因此我需要在jquery中使用.live()
之类的东西,除非它抛出错误
The problem with the above code is, the element I want to pass is generated by ajax so I need something like the .live()
in jquery, unless it throws an error
如何在jQuery中重写上述函数,以便以后也可以检测到任何DOM元素.
How do I rewrite the above function in jQuery so that any DOM element generated later can also be detected.
更新:
我的页面首次加载时,它会加载
When my page first load, it loads
1)带有代码的ajaxupload.js
1) ajaxupload.js with codes
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
2) index.php with codes
<div id="popupMargin">
<div class="close">x</div>
<div id="pop-con"></div>
</div>
<div id="ajaxGenerateMarkUp"></div>
3)现在,单击按钮,会将以下标记添加到#ajaxGeneratedmarkUp
div(通过ajax生成的标记)
3) now on the click of a button, the following markUp is added to the #ajaxGeneratedmarkUp
div (mark-up generated through 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', '<br>Uploading image please wait.....<br>'); return false;"/>
</form>
现在,在更改此输入类型文件时,在javascript中进行了调用.现在它显示了错误.
Now on change of this input type file, made the call on in the javascript. Now it shows the error.
[注意:我只发布了我认为可能会影响我的问题的代码段]
[Note: I only posted sections of code I think might affect my question]
推荐答案
jQuery的选择器可以检测到新生成的项目:
jQuery's selector can detected newly generated items:
$('#id');
所以在您的情况下:
var theVar = 'something';
$('#' + theVar); //returns a jQuery object of the select item
$('#' + theVar)[0]; //returns the first DOM object that jQuery finds with that id
这篇关于相当于`return document.getElementById(theVar)'的jQuery的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!