我有一个加载页面的'iframe'。在这个页面上它有多个链接通向外部页面,我想替换部分'href',以便它链接到'Youtube'。例如,页面上的链接看起来像这样。
http://ytchannelembed.info/video.php?id=yQGAwrtB65A&t=
我想替换
http://ytchannelembed.info/video.php?id=
用这个
http://www.youtube.com/watch?v=
以便它最终成为
http://www.youtube.com/watch?v=yQGAwrtB65A&t=
我尝试了一些jquery示例,但链接保持不变。如果有人可以提供帮助,我会非常感激。
答案 0 :(得分: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=")
答案 1 :(得分: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);