gpt4 book ai didi

javascript - iframe src 更改,Javascript 不起作用

转载 作者:行者123 更新时间:2023-12-02 19:05:26 25 4
gpt4 key购买 nike

我目前有一些 Javascript,看起来有点像这样:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script>
$(document).ready(function(){
function changeSource(newSource) {
document.getElementById('preview').src = newSource;
}
});
</script>

然后我有一些如下所示的 HTML:

<table border=0 class="colors">
<tbody>
<tr>
<td><a onclick="changeSource('http://www.test.com')"><img width="40" src="test.png"/></a></td>
</tr>
</tbody>
</table>

然后是 Iframe 部分:

<table border=0>
<tbody>
<tr>
<td><iframe id="preview" width="125" height="303" src="test.php" scrolling="no" frameborder="0" marginwidth="0" marginheight="0" hspace="0" vspace="0"></iframe></td>
</tr>
</tbody>
</table>

但是,单击标签中的图像,Iframe 的源不会更改。为什么会这样?

最佳答案

您收到错误,因为该函数是本地方法,因为您将其隐藏在 onready 函数中。该示例不需要 onready 调用。

<script>
$(document).ready(function(){
function changeSource(newSource) {
document.getElementById('preview').src = newSource;
}
});
</script>

需要

<script>
function changeSource(newSource) {
document.getElementById('preview').src = newSource;
}
</script>

更好的是,不需要 JavaScript!像应该使用的那样使用 HTML。使用目标属性。

<td><a href="http://www.example.com" target="preview"><img width="40" src="test.png"/></a></td>

关于javascript - iframe src 更改,Javascript 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14347465/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com