简体   繁体   English

使用JavaScript更改iframe src

[英]Change iframe src using JavaScript

I have three different pages; 我有三个页面。 player_lo.html , player_hi.html and player_med.html . player_lo.htmlplayer_hi.htmlplayer_med.html

These three pages are different quality flow player stream pages. 这三个页面是不同质量的流播放器流页面。 I want to use JavaScript to change an iframe 's source. 我想使用JavaScript更改iframe的源。

I have this script but it doesn't work: 我有此脚本,但它不起作用:

<a href="/player/player_lo.html" class="button grey" onclick="$('#player').attr('src', 'player_lo.html'); $(this).parent().find('a').removeClass('red'); $(this).parent().find('a').addClass('grey'); $(this).addClass('red').removeClass('grey'); return false;">Low Quality</a>
<a href="/player/player_med.html" class="button red" onclick="$('#player').attr('src', 'player_med.html'); $(this).parent().find('a').removeClass('red'); $(this).parent().find('a').addClass('grey'); $(this).addClass('red').removeClass('grey'); return false;">medium quality</a>
<a href="/player/player_hi.html" class="button grey" onclick="$('#player').attr('src', 'player_hi.html'); $(this).parent().find('a').removeClass('red'); $(this).parent().find('a').addClass('grey'); $(this).addClass('red').removeClass('grey'); return false;">high quality/a>

For an example of what I'm trying to do, look at this: http://slon.ru/tvrain/ 有关我要尝试执行的操作的示例,请查看以下内容: http : //slon.ru/tvrain/
When you click on Низкое качество - it's another frame, if next, another etc... 当您单击Низкоекачество时-这是另一个框架,如果是下一个框架,则是另一个框架...

You're using absolute paths in the href of your links (eg "/player/player_lo.html" ), but relative paths in you JavaScript (eg 'player_lo.html' ). 您在链接的href中使用绝对路径(例如"/player/player_lo.html" ),而在JavaScript中使用相对路径(例如'player_lo.html' )。 This might be your problem. 这可能是您的问题。

  • Try using the same (absolute) paths in your JavaScript. 尝试在JavaScript中使用相同(绝对)路径。
  • Make sure the pages actually exist. 确保页面实际存在。
  • Keep an eye on your browser's error console to see what is going wrong. 密切注意浏览器的错误控制台,看看出了什么问题。

Other tips you might find helpful: 您可能会发现有用的其他提示:

Instead of two classes called red and grey (which are mutually exclusive classes, meaning a button can't be both at the same time), try using a single class which describes the difference, eg active . 代替使用称为红色灰色的两个类(它们是互斥的类,这意味着按钮不能同时使用),请尝试使用描述差异的单个类,例如active Then, use CSS to style buttons to be grey by default, or red when they have the class active . 然后,使用CSS将按钮的样式设置为默认(默认)为灰色,或将按钮设置为active时设置为红色。 Added advantage is that the colours are not encoded in the class names (eg you can change the active color to blue, or whatever, in the future). 额外的优点是,颜色未在类名称中编码(例如,将来可以将活动颜色更改为蓝色,或其他颜色)。

Also, instead of using the inline onclick attribute, try binding the event hander to the <a> tags from JavaScript itself . 另外,不要使用内联onclick属性,而是尝试将事件处理程序绑定到JavaScript本身<a>标记。 That way, you can easily use the <a> 's existing href , plus you can reuse the code needed to make the clicked button active. 这样,您可以轻松使用<a>的现有href ,还可以重用使单击的按钮处于活动状态所需的代码。

HTML: HTML:

<a href="/player/player_lo.html" class="button">Low Quality</a>
<a href="/player/player_med.html" class="button">medium quality</a>
<a href="/player/player_hi.html" class="button">high quality/a>

JavaScript: JavaScript:

$('.button').click(function(event) {
    var button = $(this);
    $('#player').attr('src', button.attr('href'));
    makeActive(button);
    return false;
});

function makeActive(button) {
    $('.button').removeClass('active');
    button.addClass('active');
}

CSS: CSS:

.button {
    color: grey;
}
.button.active {
    color: red;
}

Documentation: 说明文件:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM