考虑以下Jquery外部脚本片段,用于在模态窗口中创建youtube iframe:
$(function() {
$(document).ready(function() {
var myPhpVa = '<?php echo embeddable($ytid);?>';
$('a[name="modal"]').click(function(e) {
$('<iframe width="560" height="315" src=myPhpVa frameborder="0" allowfullscreen>').appendTo('#dcontent');
}
});
出于某种原因,我找不到对象。 PHP函数在此javascript之上声明,声明为:
function embeddable($ytid){
return $embeddable = 'http://www.youtube.com/embed/'.$ytid;
}
非常感谢任何传递此PHP变量的帮助。
答案 0 :(得分:3)
首先 - 分配返回值是没用的。删除它:
function embeddable($ytid){
return 'http://www.youtube.com/embed/'.$ytid;
}
关于$(document).ready(function() {
和$(function() {
的相同内容 - 它们相等且加倍 - 删除其中一个。
指定myPhpVa
变量后 - 写
alert(myPhpVa);
并查看它显示的内容。
答案 1 :(得分:2)
我认为将iframe附加到html的代码语法不正确。
$('<iframe width="560" height="315" src="' + myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent');
答案 2 :(得分:1)
看起来你没有突破字符串以将myPhpVa
连接到js文字中。试试这个:
$('<iframe width="560" height="315" src="' + myPhpVa + '" frameborder="0" allowfullscreen>').appendTo('#dcontent');
如果这不起作用,请提醒myPhpVa
的值,看看它是否符合预期。