[英]How to get another child element by classname of the parent div of a element that triggered a function
my problem sounds pretty simple. 我的问题听起来很简单。 I have divs with no id and class specified like this
我有没有像这样指定的id和类的div
<div style="position:relative; float:left">
<input type="text" onfocus="renderCalendar(event);" size="50" id="cal" name="cal">
<div class="wrappercal calwrapper">
<div class="yearmonth">
<table class="caltable"><tbody>
<tr><th width="80%" scope="col" colspan="5">
<select class="dtcombo year" onchange="changeCalendar()">
<option value="2000">2000</option>
<option value="2001">2001</option>
</select>
<select onchange="changeCalendar()" class="dtcombo month">
<option value="1">1</option>
<option value="2">2</option>
</select></th>
</tr>
</tbody>
</table>
</div>
</div>
</div>
There may be several similar divs in the page 页面中可能有几个类似的div
I have a javascript function 我有一个javascript函数
function renderCalendar(event){
var triggerer=event.target.id; // THIS WILL GIVE THE ID OF TEXTBOX THAT TRIGGERED THIS FUNCTION.
$("#"+triggerer).parent().children(".wrappercal").children(".yearmonth").children(".caltable").children(".year").val(); //not working
}
The nest follows like this 巢跟随这样
Parent Div > wrappercal(div) > yearmonth(div) > caltable(table) > year(select)
my problem is to get value of combo with class "year" of the parent div of textbox that triggered the function. 我的问题是获取触发该函数的文本框的父div的类“year”的组合值。
Can anybody figure out where i went wrong? 任何人都可以弄明白我哪里出错了吗? any suggestions?
有什么建议么?
PS Please bear I m not good with dom elements. PS请承担我对dom元素的不满意。
Thanks 谢谢
Why not try this? 为什么不尝试这个?
function renderCalendar(event){
$(event.target).parent().find(".wrappercal .yearmonth .caltable .year").val();
}
Can you check (using console.log(event.target)
and tracking) that the target element is really what/where it should be? 你能检查(使用
console.log(event.target)
和跟踪)目标元素究竟是什么/应该在哪里?
EDIT 编辑
The problem is that children()
only searches through one level (deep) of child nodes and there is multiple levels between .caltable
and .year
( <tr><th>...
). 问题是
children()
只搜索一级(深)子节点,并且.caltable
和.year
之间有多个级别( <tr><th>...
)。 Use find()
instead (which will search recursively through your nodes). 改为使用
find()
(将以递归方式搜索节点)。 ;-) ;-)
Try the Following 尝试以下
function renderCalendaer(event) {
$("#"+event.target.id).parent().find(".year").val();
}
你可以试试
$("#"+triggerer).parent().find(".year").val() instead.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.