【问题标题】:Changing attr value with Jquery使用 Jquery 更改 attr 值
【发布时间】:2013-07-09 18:22:22
【问题描述】:

我正在尝试在单击链接时更改 iframe 的 src 值,并使用其中的 href 更改值。单击按钮时,我会阻止默认操作,并将 src 值替换为 url。该代码适用于不是 url 的值,导致 iframe 显示未找到的页面,但在提供有效 url 时不起作用。这是正在使用的页面的代码。

这是我用来运行网站的主要脚本

$.ajaxSetup ({  
    cache: false  
    });  
$(document).ready(function(){  

$('.pageContent').load('home.php');

setInterval("changePage()",250); 

});
function changePage(hash)
{

   $('.youtubeLink').click(function(e) {

    e.preventDefault();
    var temp = $(this).attr("href");
    //alert (temp);
    $('#youtubePlayerFrame').attr('src', temp);

//when using var temp no action occurs
//when testing temp, it does hold the url, if i replace temp with a string such as   
//'asdasd' the script works correctly but replaces the frame with a 404 error
})

  $(window).hashchange( function(){

    if(!hash) hash=window.location.hash;
    hash = hash.replace('#','');
    if (hash =='')
  {
  $('.pageContent').load('home.php');

  }

}
else
{
$('.pageContent').load( hash + '.php');
hash = null;
}
})
}

此代码是保存 youtube 播放器的主 div

<?php

echo '

<div class="youtubePlayer" >
<iframe width="420" height="315" id = "youtubePlayerFrame"   src="http://www.youtube.com/embed/j8Szl_JyCUQ" frameborder="0" allowfullscreen style="margin-top:34px; margin-left:20px;"></iframe>
</div>

';

include 'sideDirectory.php';
include 'sideMenu.php';


?>

最后一段代码是带有链接的目录的 php 文件

<?php
echo'

<div class="sideDirectory">
 <table><tbody>
 ';
 $file = fopen("../data/recentlyAdded.txt","r");

 $linksFile = fopen("../data/links.txt","r");

 while($line =fgets($file))
{

  $url = fgets($linksFile);
//   echo '<tr><td>' .$url. '</td></tr>';
  echo '<tr><td><a class="youtubeLink" href="'.$url.'">'.$line.'</a></td></tr>';     
}
fclose($file);
fclose($linksFile);
echo '
</tbody> </table>
</div>
';
?>

【问题讨论】:

  • 天哪,你每秒绑定一个新的点击事件四次,这对你来说怎么样?
  • 哇哇哇!它是一个 iframe。这可能与您浏览器中的某些同源策略有关吗?
  • 为什么不让你的锚标签target iframe? target="iframeid"
  • 查看同源策略
  • 我也在尝试定位 iframe,目前似乎无法正常工作

标签: php jquery html


【解决方案1】:

试试这样吧:

$(document).ready(function(){
    $.ajaxSetup ({  
        cache: false  
    });  
    $('.pageContent').load('home.php')
                     .on('click', '.youtubeLink', function(e) {
                        e.preventDefault();
                        $('#youtubePlayerFrame').prop('src', this.href);
                     });

    $(window).on('hashchange', function(){
        var hash = window.location.hash.replace('#','');
        $('.pageContent').load( (hash.length ? hash : 'home') + '.php');
    });
});

绑定事件处理程序在每秒运行四次的间隔中确实没有位置,它们只需要绑定一次即可工作。

【讨论】:

  • 感谢您指出这一点并清理代码,仍在通过反复试验学习。它仍然没有正确加载框架,只是坐在那里。这是链接,如果有人想亲自查看musicsharing.co.nf/pages/home.html,然后单击标题中的圆圈按钮
  • .youtubelink#youtubePlayerFrame 在哪里?这些是你创造的元素,它们不在 Youtube 上?
  • 没有该目录中的链接在 .youtubelink 类下,并且框架 ID 是#youtubePlayerFrame,我正在尝试将 url 加载到
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-04-01
  • 1970-01-01
  • 2014-07-26
  • 1970-01-01
  • 2013-06-21
  • 1970-01-01
  • 2012-01-08
相关资源
最近更新 更多