Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/453.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用javaScript访问父div中的元素?_Javascript_Html - Fatal编程技术网

使用javaScript访问父div中的元素?

使用javaScript访问父div中的元素?,javascript,html,Javascript,Html,嗨,我想访问所有功能,只需在一个div中执行即可! 例如: <div class="ow_chat_dialog" id="main_tab_contact_11"> <iframe id="myframe"></iframe> <button onclick="sersrc()"></button> </div> <div class="ow_chat_dialog" id="main_tab_contact_12

嗨,我想访问所有功能,只需在一个div中执行即可! 例如:

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

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

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

14
15
16
.
.
....
函数setscr(){
document.findviewbyd(“myframe”).src=“hrl…”
}
在这段代码中,我可以创建一些具有一个类名和id的div。。。 但是当我点击任何一个按钮时,第一个iframe就改变了! 如果我想更改兄弟iframe!
**只需使用javaScript,而不是Jquery。

不要使用ID,它们必须是唯一的。改用类:

<div class="ow_chat_dialog" class="main_tab_contact_13">
    <iframe class="myframe"></iframe>
    <button onclick="sersrc(this)"></button>
</div>
如果您确定iframe总是在按钮的前面,那么您可以简单地执行以下操作

obj.previousElementSibling.src = "hrl..."
(当然,这不太可靠,因为这取决于结构)。

var els=document.querySelectorAll('.ow\u chat\u dialog>button');
函数处理程序(){
this.previousElementSibling.src=“//stackoverflow.com”;
}

对于(var i=0;iinvalid html,重复id
myframe
那么,我如何解决它呢?这个div是由用户创建的…未捕获的TypeError:无法读取未定义的属性'querySelector',您传递
这个
参考:
onclick=“sersrc(this)”
?我使用这个:var frame=this.parentNode.querySelector(.myframe”);frame.contentWindow.location.href='localhost:8585/demos/project/index.html';您应该使用
obj.parentNode
,因为
这个
不是按钮,而是全局对象
window
。另一个错误:未捕获类型错误:无法设置null的属性'src',我如何在函数中使用您的代码?如果是进一步的话,我们有预处理吗javaScript中的viousElementSibling?如果我更改iframe和button!首先是button,然后是iframe,我可以使用你的代码吗?我想在button onclick中使用你的javaScript代码!避免内联事件侦听器。只需在单击
处理程序
函数时编写你想要运行的代码。未捕获类型错误:无法设置Undefinedy的属性“src”方法有误。
此。以前的元素同级
不能是
未定义的
obj.previousElementSibling.src = "hrl..."