Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript从替换为_Javascript_Jquery_Str Replace - Fatal编程技术网

Javascript从替换为

Javascript从替换为,javascript,jquery,str-replace,Javascript,Jquery,Str Replace,我找到了关于如何将字符串替换为字符串的所有内容的好教程,但是如何将a替换为b呢 假设我们有这个选择器 $('a.mylink') 此选择器将返回如下内容 <a class=​"micro_avatar my_avatar">​</a>​ <a href=​"#!/​users/​user_name" style=​"background:​ url(https:​/​/​secure.gravatar.com/​avatar/​randomnumber?s

我找到了关于如何将字符串替换为字符串的所有内容的好教程,但是如何将a替换为b呢

假设我们有这个选择器

$('a.mylink')
此选择器将返回如下内容

<a class=​"micro_avatar my_avatar">​</a>​ 
<a href=​"#!/​users/​user_name" style=​"background:​ 
  url(https:​/​/​secure.gravatar.com/​avatar/​randomnumber?size=24&default=mm)​ 
  no-repeat" class=​"micro_avatar">​</a>​
http://www.youtube.com/watch?v=_sgu7CioctU
​
http://www.youtube.com/watch?v=_sgu7CioctU
在我测试这个的页面中,会发现这个w变体的多个实例(想象一个博客和每个帖子都有这个)

现在我们想把所有的眼影都换成这样

<a class=​"micro_avatar my_avatar">​</a>​ 
<a ** rel='nofollow' ** href=​"#!/​users/​user_name" style=​"background:​ 
  url(https:​/​/​secure.gravatar.com/​avatar/​randomnumber?size=24&default=mm)​ 
  no-repeat" class=​"micro_avatar">​</a>​

**<iframe width="560" height="315" src="** http://www.youtube.com/embed/_sgu7CioctU
**" frameborder="0" allowfullscreen></iframe>**
​
****
诀窍在于并非所有youtube链接都具有相同的链接。

使用

试试这个

更新

HTML

<span class="youtube_url">http://www.youtube.com/embed/_sgu7CioctU</span>
http://www.youtube.com/embed/_sgu7CioctU
脚本

$('.micro_avatar:not(.my_avatar)').attr('rel',nofollow);
$('<iframe>', {
   src: $('.youtube_url').text(),
   id:  'myFrame',
   width: 560,
   allowfullscreen: true,
   height:350
}).appendTo('body');
$('.micro\u avatar:not(.my\u avatar)).attr('rel',nofollow);
$('', {
src:$('.youtube_url').text(),
id:'我的框架',
宽度:560,
allowfullscreen:是的,
身高:350
}).附于(“主体”);

“这将返回4”-我不明白。数值4没有意义,四个元素没有意义,因为选择器
'a.mylink'
与您显示的html不匹配。
$('a.mylink')
与问题的其余部分有什么关系?你是说第一个代码块实际上是一个字符串的内容,你想把它改成第二个,还是在文档正文的某个地方,或者…?这里的问题是youtube Url不一定是同一个,所以我们必须解析每个案例的Url id。你应该将
youtube Url
存储在一个元素中,然后您可以获取
脚本
。检查上面的答案,我对它做了修改。
$('.micro_avatar:not(.my_avatar)').attr('rel',nofollow);
$('<iframe>', {
   src: $('.youtube_url').text(),
   id:  'myFrame',
   width: 560,
   allowfullscreen: true,
   height:350
}).appendTo('body');