2017-03-23 29 views
0

我想根据点击的链接触发不同来源的iframe。我到达的情况下,我可以点击不同链接,我管理,以火一样的iframe中,我硬编码象下面这样:如何在jQueryFunction中将变量传递到iFrame中

这是什么在起作用:

 $('.myLink').click(function(){ 
     var myFile = $(this).attr('videofile'); 
     var myPost = $(this).attr('myfile'); 
     var myWidth = Number($(this).attr('mywidth')); 
     var myHeight = Number($(this).attr('myheight')); 

     var myCode = '<iframe height="800" width="800"   
     src="https://www.duolingo.com/" allowfullscreen></iframe>'; 

     $('#myPlayer').html(myCode);` 

然而,当我想通过我的变量放到我的iframe中,以便根据指定变量应用不同的来源,高度和宽度,即使变量定义良好,它也不起作用。它是不接受它们的iframe。

 $('.myLink').click(function(){ 
     var myFile = $(this).attr('myfile'); 
     var myPost = $(this).attr('myfile'); 
     var myWidth = Number($(this).attr('mywidth')); 
     var myHeight = Number($(this).attr('myheight')); 

     var myCode = '<iframe height="+'myHeight'+" width="+'myWidth'+" 
     src="+'myFile'+" allowfullscreen></iframe>'; 

     $('#myPlayer').html(myCode);` 

`

我的问题是如何修改/写代码mycode的变量,这样我可以通过MYFILE,myWidth,myHeight变量引入IFRAME就像我在mycode的变量上面那样做的iframe工作?它不适用于我传递变量的方式,我很确定我正确传递了它们。

回答

0

我觉得你的引号有点混乱。 ES6引入template literals,这使得这一点更容易。

这里的东西,你可以捣鼓(HOHO)有:

const height = 100; 
const width = 500; 
$('#foo').html(`<iframe height="${height}" width="${width}" src="https://www.bing.com"></iframe>`); 

https://jsfiddle.net/y19ma974/3/

+0

感谢的建议,但它仍然无法正常工作时,iframe被炒鱿鱼因为如果没有值传递。然而,当我硬编码的源代码(src =“https://www.bing.com”),iframe的作品,但没有变量的宽度和高度应用。就像在我的引用情况下,这很好,因为我成功传入的变量触发了其他代码。它只是不工作的iframe。看起来,iframe不容许在jQuery中传递变量 – Voytek