我有一个SELECT html元素,它具有如下的onchange事件:

onchange="myFunction(this)"


在这种情况下,这是什么对象?

function myFunction(control){
var jqObject = $(control);
...
..
var jqObject2 = jqObject.find('select.mySecondSelect')
}


在我的函数中,我得到了另一个带有jQuery(jqObject2)的对象,该对象表示另一个select HTML元素,我想将此对象级联为类似的函数,例如myFunction(this)

那么,如何将jQuery对象转换为JavaScript对象,但是将其作为参数呢?

我在一些帖子中发现我应该需要调用jqObject.get(0)或jqObject [0],但是我认为这不是同一回事。因为使用get(0)我获得了本机DOM元素,但没有专门作为对象AFAIK。



更新资料

这将是整个场景:

的HTML

<select name="lstCountry" class="country" onchange="onCountryChange(this)">
  <option value="USA">USA</option>
  <option value="MEXICO">Mexico</option>
</select>

<select name="lstState" class="state" onchange="onStateChange(this)">
....
</select>


JS

function onCountryChange(control){
   var oCountry = $(control);
   var oState = oCountry.closest('tr').find('select.state'); //<== oState is never empty or unspecified. I always get the object correctly.
   //do operations related to the country

   //specific case, call the onStateChange
   onStateChange(oState.get(0));  //<== Here!!
}

function onStateChange(control){
   var oState = $(control); //<== When I call this from the function onCountryChange, control is equals to unspecified. BUT not when the user really perform the onchange on the control state (select Element) manually
   //do operations related to the state
}

最佳答案

this属性中的onXXX是DOM元素本身。因此,在这种情况下,用户更改的是<select>元素。 $(control)创建一个包含DOM元素的jQuery对象。给定一个包含元素集合的jQuery对象,您可以使用jqObject.get(n)jqObject[n]获取该集合中的第n个元素。因此,如果jQuery对象中只有一个元素,则jqObject[0]将返回该元素。

关于javascript - 什么是“this”作为JavaScript函数中的参数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32384181/

10-09 22:26