如何使用变量填充iframe源?
16lz
2021-01-22
I am trying to set my current iframe source with a variable that I created but it doesn't seem to work. I figured that my JavaScript syntax is wrong.
我正在尝试使用我创建的变量设置我当前的iframe源,但它似乎不起作用。我认为我的JavaScript语法错误。
My script is:
我的脚本是:
<script type="text/javascript">
$(document).ready(function(){
var iframeAttributes = window.location.search;
var iframeForm = 'www.test.com/form.html';
var iframeURL = iframeForm + iframeAttributes;
$('.testframe').attr('src', iframeURL);
});
</script>
My IFrame code is:
我的IFrame代码是:
<iframe id="testframe" src="" width="600" height="586" frameBorder="0">></iframe>
3 个解决方案
#1
1
Your selector is wrong:
你的选择器错了:
$('.testframe').attr('src', iframeURL);
This tries to find elements with class testframe
but you have set the ID
in your HTML
, not the class.
这会尝试使用类testframe查找元素,但您已在HTML中设置了ID,而不是类。
Use this to select the element with id testIframe
:
使用它来选择id为testIframe的元素:
$('#testframe').attr('src', iframeURL);
更多相关文章
- 使用JavaScript多态在子类中创建唯一变量而不覆盖
- javascript入门笔记(1)——变量和计算
- 有没有办法检查两个数组是否具有相同的元素?
- 如何从json对象获取匹配元素的索引?
- 从单击使用属性作为变量进行AJAX调用。
- 为什么我不能删除数组的元素?
- jQuery插件:如何将元素引用传递给回调函数?
- 在JavaScript中访问PHP变量[重复]
- js的html元素的父节点,子节点