使用 jquery 替换 href 的一部分

Replace part of an href using jquery

本文关键字:一部分 href 替换 jquery 使用      更新时间:2023-09-26

我有一个加载页面的'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示例,但链接保持不变。如果有人能帮忙,我将不胜感激。

不需要

使用 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=")

这可以通过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);