Javascript 使用jquery替换href的一部分

Javascript 使用jquery替换href的一部分,javascript,php,jquery,html,youtube,Javascript,Php,Jquery,Html,Youtube,我有一个“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.you

我有一个“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=")

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

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

无需使用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);
//带有URL的数组
var url=数组(“http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2");
//声明新的url数组
var new_url=Array();
//循环浏览所有URL

对于(i=0;i),这可以通过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);
//带有URL的数组
var url=数组(“http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2");
//声明新的url数组
var new_url=Array();
//循环浏览所有URL

对于(i=0;i),这可以通过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);
//带有URL的数组
var url=数组(“http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2");
//声明新的url数组
var new_url=Array();
//循环浏览所有URL

对于(i=0;i),这可以通过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);
//带有URL的数组
var url=数组(“http://ytchannelembed.info/video.php?id=XXXXXXXX1","http://ytchannelembed.info/video.php?id=XXXXXXXXX2");
//声明新的url数组
var new_url=Array();
//循环浏览所有URL



对于(i=0;i可能您可以发布您尝试过的内容。我尝试过
$('a')。每个(函数(){this.href=this.href.replace('ytchannelembed.info/video.php?id=','www.youtube.com/watch?v=');)
@Sabre,所以它应该替换它。您在其他地方做错了什么,可能没有在正确的时间调用代码。如何调用此代码?我使用了
$(document).ready(function(){
@Sabre,所以提供一个JSFIDLE来复制您的问题(如果可以的话)因为您在这里看到的所有答案都是与您已经使用的代码完全相同的代码…也许您可以发布您已经尝试过的内容。我已经尝试了
$('a')。每个(function(){this.href=this.href.replace('ytchannelembed.info/video.php?id=','www.youtube.com/watch?v=');)
@Sabre,所以它应该替换它。您在其他地方做错了什么,可能没有在正确的时间调用代码。如何调用此代码?我使用了
$(document).ready(function(){
@Sabre,所以提供一个JSFIDLE来复制您的问题(如果可以的话)因为您在这里看到的所有答案都是与您已经使用的代码完全相同的代码…也许您可以发布您已经尝试过的内容。我已经尝试了
$('a')。每个(function(){this.href=this.href.replace('ytchannelembed.info/video.php?id=','www.youtube.com/watch?v=');)
@Sabre,所以它应该替换它。您在其他地方做错了什么,可能没有在正确的时间调用代码。如何调用此代码?我使用了
$(document).ready(function(){
@Sabre,所以提供一个JSFIDLE来复制您的问题(如果可以的话)因为您在这里看到的所有答案都是与您已经使用的代码完全相同的代码…也许您可以发布您已经尝试过的内容。我已经尝试了
$('a')。每个(function(){this.href=this.href.replace('ytchannelembed.info/video.php?id=','www.youtube.com/watch?v=');)
@Sabre,所以它应该替换它。您在其他地方做错了什么,可能没有在正确的时间调用代码。如何调用此代码?我使用了
$(document).ready(function(){
@Sabre,所以提供一个JSFIDLE来复制您的问题(如果可以的话)因为你在这里看到的所有答案都与你正在使用的代码完全相同……谢谢你的答案,但我有多个链接需要
http://ytchannelembed.info/video.php?id=
已替换。感谢您的回答,但我有多个链接需要
http://ytchannelembed.info/video.php?id=
已替换。Tha谢谢你的回答,但我有多个链接需要
http://ytchannelembed.info/video.php?id=
已替换。感谢您的回答,但我有多个链接需要
http://ytchannelembed.info/video.php?id=
已替换。谢谢你的回答。如果你看一下这个,应该可以帮助解释我的问题。我仍然不知道站在你想得到的地方。@PhilipG他想修改跨域iframe的内容…通常从一开始就是一个不清楚的问题抱歉,如果我没有说清楚,但我想替换所有
http://ytchannelembed.info/video.php?id=
http://www.youtube.com/watch?v=
自动,因为我无法更改每个规格由于网页定期接收新视频,所以有一个IFI链接。@Sabre,但是这些链接是否在跨域iframe中像在你的JSFIDLE中一样???我希望你能更清楚地回答这个问题。答案应该是肯定或否定谢谢你的回答。如果你看了这个,应该可以帮助解释我的问题。我仍然不明白你在说什么nt到achive。@PhilipG他想修改跨域iframe的内容…通常从一开始就是一个不清楚的问题抱歉如果