2013-12-18 92 views
0

我有一个加载页面的'iframe'。在这个页面上,它有多个链接导致外部页面,我想替换部分'href',以便它链接到'Youtube'。例如,页面上的链接如下所示。用jquery替换部分href

http://ytchannelembed.info/video.php?id=yQGAwrtB65A&t=

我想这个

http://www.youtube.com/watch?v=

更换

http://ytchannelembed.info/video.php?id=

,使其最终成为

http://www.youtube.com/watch?v=yQGAwrtB65A&t=

我已经尝试了一些jquery的例子,但链接只是保持不变。如果有人可以帮助,我会非常感激。

+1

也许你可以发布你已经尝试过。 – Florent

+0

我试过'$('a')。each(function(){this。this = this.href.replace('ytchannelembed.info/video.php?id=','www.youtube.com/watch ?v ='); });' – Sabre

+0

@Sabre所以它应该取代它。你在别的地方做错了什么,或许不能在正确的时间调用代码。你如何称此代码? –

回答

2

无需使用jQuery,试试这个:

var url = "http://ytchannelembed.info/video.php?id=yQGAwrtB65A&t="; 
url = url.replace("http://ytchannelembed.info/video.php?id=","http://www.youtube.com/watch?v=") 
+0

谢谢你的回答,但我有多个链接需要'http: //ytchannelembed.info/video.php?id='替换。 – Sabre

1

这既可以实现JavaScript和PHP

PHP:

<?php 
//Array with urls 
$url = array("http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2"); 
//Loop through urls 
foreach($url as $u){ 
    //Dump new URLS to new array 
    $new_url[] = str_replace("http://ytchannelembed.info/video.php? id=","http://www.youtube.com/watch?v=", $u); 
} 
//Dump all new URLS 
var_dump($new_url) 
?> 

的Javascript:

//Array with urls 
var url = Array("http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2"); 
//Declare new_url array 
var new_url = Array(); 

//Loop through all urls 
for(i=0; i<url.length; i++){ 
    //Push new URLS to new array 
    new_url.push(url[i].replace("http://ytchannelembed.info/video.php? id=","http://www.youtube.com/watch?v="); 
} 
//alert all new urls 
alert(new_url); 
+0

谢谢你的回答。如果你看看这个http://jsfiddle.net/Hck4a/它应该有助于解释我的问题。 – Sabre

+0

我还是不明白你想要一个韭菜。 –

+0

@PhilipG他想修改跨域iframe的内容...通常情况下,从一开始就不清楚的问题 –