如何在javaScript中访问带有ID的最近元素? [英] How acces to nearest element with an ID in javaScript ?

查看:252
本文介绍了如何在javaScript中访问带有ID的最近元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有div得到了!所有div都是一个!示例:

 <   div  < span class =code-attribute>   class   =  ow_chat_dialog    id   =   main_tab_contact_11 >  
< iframe id = myframe > < span class =code-keyword>< / iframe >
< 按钮 onclick = setsrc( ) > < / button >
< / div >

< div class = ow_chat_dialog id = main_tab_contact_12 >
< iframe id = myframe > < / iframe >
< button onclick = setsrc() > < / button >
< / div >

< div class = ow_chat_dialog id = main_tab_contact_13 >
< iframe id = myframe > < / iframe >
< 按钮 onclick = setsrc() > < / button >
< / div >
14
15
16


....
< script >
function setscr(){
document .findviewbyid( myframe)。src = hrl ...
}
< / script >





但是我点击按钮更改第一个iframe元素!

请帮助我,我需要帮助:((

解决方案

首先,你的HTML是非法的。因为只有一个ID允许带有指定的标识符字符串。你的有3个。将HTML改为这个。



 <   div     class   =  ow_chat_dialog    id   =  main_tab_contact_11 >  
< iframe class = < span class =code-keyword> myframe > < / iframe >
< 按钮 onclick = setsrc() > < / button >
< / div >

< div class = ow_chat_dialog id = main_tab_contact_12 >
< iframe class = myframe > < / iframe >
< 按钮 onclick = setsrc() > < / button >
< / div >

< div class = ow_chat_dialog id = main_tab_contact_13 >
< iframe class = myframe > < / iframe >
< 按钮 onclick = setsrc() > ; < / button >
< / div > ;





这就是您使用类对相同类型的元素进行分组的原因。它首先选择的问题是因为它开始寻找具有myframe ID的任何元素,并且它找到的第一个元素应用了资源。



其次,要获得最近的,或iframe在同一个父级中,您可以使用以下任何一种方法,请使用jQuery使事情变得简单。



从同一父母那里寻找



 


document )。ready( function () {


' button')。click( function (){
// 删除事件hanlder来自HTML标记
// 在此处理..

Hi i have div that reapited ! all div are one ! Example :

<div class="ow_chat_dialog" id="main_tab_contact_11">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>

<div class="ow_chat_dialog" id="main_tab_contact_12">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>

<div class="ow_chat_dialog" id="main_tab_contact_13">
<iframe id="myframe"></iframe>
<button onclick="setsrc()"></button>
</div>
14
15
16
.
.
....
<script>
function setscr(){
document.findviewbyid("myframe").src="hrl..."
}
</script>



But wen i click on button change the first iframe element !!!
plz help me i need help :((

解决方案

First things first, you HTML is illegal. Because there is only one ID allowed with a specified identifier string. Yours has 3 of them. Change the HTML to this one,

<div class="ow_chat_dialog" id="main_tab_contact_11">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>
 
<div class="ow_chat_dialog" id="main_tab_contact_12">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>
 
<div class="ow_chat_dialog" id="main_tab_contact_13">
   <iframe class="myframe"></iframe>
   <button onclick="setsrc()"></button>
</div>



This is why you use class to group elements of same type. The problem that it selected the first, was because it started looking for any element with an ID of myframe, and the first one it found was applied with the source of resource.

Secondly, to get the nearest, or the iframe inside the same parent you can use any of the following methods, do use jQuery to make things a little simple.

Finding from the same parent


(document).ready(function () {


('button').click(function () { // remove the event hanlder from the HTML markup // handle it here..


这篇关于如何在javaScript中访问带有ID的最近元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆