【问题标题】:remove all anchor tags from innerHTML or link to wikipedia.com从 innerHTML 中删除所有锚标记或链接到 wikipedia.com
【发布时间】:2013-12-12 09:15:54
【问题描述】:
success:function(message){
                var s = JSON.parse(message);
                document.getElementById('search').innerHTML=s.parse.text['*'];
        }

我想提一下

    s.parse.text['*']="<div class="dablink">This article is about the Indian film actor.` For other people named Salman Khan, see <a href="/wiki/Salman_Khan_(disambiguation)" title="Salman Khan (disambiguation)">Salman Khan (disambiguation)</a>.</div> <div class="metadata topicon nopopups" id="protected-icon" style="display:none; right:55px;"><a href="/wiki/Wikipedia:Protection_policy#semi" title="This article is semi-protected to promote compliance with the policy on biographies of living people."><img alt="Page semi-protected" src="//upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Padlock-silver.svg/20px-Padlock-silver.svg.png" width="20" height="20" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Padlock-silver.svg/30px-Padlock-silver.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/f/fc/Padlock-silver.svg/40px-Padlock-silver.svg.png 2x" /></a></div> <table class="infobox biography vcard" cellspacing="3" style="border-spacing:3px;width:22em;"> <tr> <th colspan="2" style="text-align:center;font-size:125%;font-weight:bold;"><span class="fn">Salman Khan</span></th> </tr> <tr> <td colspan="2" style="text-align:center;"><a href="/wiki/File:Salmanrampwalk.png" class="image" title="Salman Khan"><img alt="Salman Khan" src="//upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Salmanrampwalk.png/220px-Salmanrampwalk.png" width="220" height="342" srcset="//upload.wikimedia.org/wikipedia/commons/d/dd/Salmanrampwalk.png 1.5x, //upload.wikimedia.org/wikipedia/commons/d/dd/Salmanrampwalk.png 2x" /></a><br /> <div>Salman Khan in 2009</div> </td> </tr> <tr> <th scope="row" style="text-align:left;">Born</th> <td><span class="nickname">Abdul Rashid Salim Salman Khan</span><br /> <span style="display:none">(<span class="bday">1965-12-27</span>)</span> 27 December 1965 <span class="noprint ForceAgeToShow">(age&#160;47)</span><br /> <span class="birthplace"><a href="/wiki/Indore" title="Indore">Indore</a>, Madhya Pradesh, India</span></td> </tr> <tr> <th scope="row" style="text-align:left;">Residence</th> <td class="label"><a href="/wiki/Bandra" title="Bandra">Bandra</a>, <a href="/wiki/Mumbai" title="Mumbai">Mumbai</a><br /> <a href="/wiki/Panvel" title="Panvel">Panvel</a><sup id="cite_ref-indiatoday.intoday.in_1-0" class="reference"><a href="#cite_note-indiatoday.intoday.in-1"><span>[</span>1<span>]</span></a></sup></td> </tr> <tr> <th scope="row" style="text-align:left;">Occupation</th> <td class="role">Film actor<br /> Television presenter<br /> Model<br /> Philanthropist</td> </tr> <tr> <th scope="row" style="text-align:left;">Years&#160;active</th> <td>1988–present</td> </tr> <tr> <th scope="row" style="text-align:left;">Religion</th> <td class="category"><a href="/wiki/Islam" title="Islam">Islam</a> and <a href="/wiki/Hinduism" title="Hinduism">Hinduism</a><sup id="cite_ref-2" class="reference"><a href="#cite_note-2"><span>[</span>2<span>]</span></a></sup></td> </tr> <tr> <th scope="row" style="text-align:left;">Parents</th> <td><a href="/wiki/Salim_Khan" title="Salim Khan">Salim Khan</a> (Father)<br /> Sushila Charak Khan (Mother)</td> </tr> <tr> <th scope="row" style="text-align:left;">Relatives</th> <td><a href="/wiki/Arbaaz_Khan_(Indian_actor)" title="Arbaaz Khan (Indian actor)">Arbaaz Khan</a> (Brother)<br /> <a href="/wiki/Sohail_Khan" title="Sohail Khan">Sohail Khan</a> (Brother)<br /> <a href="/wiki/Helen_(actress)" title="Helen (actress)">Helen Richardson Khan</a> (Step-mother)<br /> <a href="/wiki/Malaika_Arora_Khan" title="Malaika Arora Khan">Malaika Arora Khan</a> (Sister-in-law)<br /> <a href="/wiki/Atul_Agnihotri" title="Atul Agnihotri">Atul Agnihotri</a> (Brother-in-law)</td> </tr> </table> <p><b>Salman Khan</b> (pronunciation&#160;: <span title="Representation in the International Phonetic Alphabet (IPA)" class="IPA"><a href="/wiki/Help:IPA_for_Hindi_and_Urdu" title="Help:IPA for Hindi and Urdu">[səlˈmaːn ˈxaːn]</a></span>; born <b>Abdul Rashid Salim Salman Khan</b> on 27 December 1965)<sup id="cite_ref-3" class="reference"><a href="#cite_note-3"><span>[</span>3<span>]</span></a></sup> is an <a href="/wiki/India" title="India">Indian</a> actor, producer, television presenter, and philanthropist. He is the son of actor and screenwriter <a href="/wiki/Salim_Khan" title="Salim Khan">Salim Khan</a>, Khan began his acting career with <i><a href="/wiki/Biwi_Ho_To_Aisi" title="Biwi Ho To Aisi">Biwi Ho To Aisi</a></i> but it was his second film <i><a href="/wiki/Maine_Pyar_Kiya" title="Maine Pyar Kiya">Maine Pyar Kiya</a></i> in which he acted in a lead role that garnered him the <a href="/wiki/Filmfare_Award_for_Best_Male_Debut" title="Filmfare Award for Best Male Debut">Filmfare Award for Best Male Debut</a>. Khan has starred in several commercially successful films, such as <i><a href="/wiki/Saajan" title="Saajan">Saajan</a></i> (1991), <i><a href="/wiki/Hum_Aapke_Hain_Koun..!" title="Hum Aapke Hain Koun..!">Hum Aapke Hain Koun..!</a></i> (1994), <i><a href="/wiki/Karan_Arjun" title="Karan Arjun">Karan Arjun</a></i> (1995), <i><a href="/wiki/Judwaa" title="Judwaa">Judwaa</a></i> (1997), <i><a href="/wiki/Pyaar_Kiya_To_Darna_Kya_(1998_film)" title="Pyaar Kiya To Darna Kya (1998 film)">Pyar Kiya To Darna Kya</a></i> (1998), <i><a href="/wiki/Biwi_No.1" title="Biwi No.1">Biwi No.1</a></i> (1999), and <i><a href="/wiki/Hum_Saath_Saath_Hain" title="Hum Saath Saath Hain" class="mw-redirect">Hum Saath Saath Hain</a></i> (1999), having appeared in the highest grossing film nine separate years during his career, a record that remains unbroken.<sup id="cite_ref-4" class="reference"><a href="#cite_note-4"><span>[</span>4<span>]</span></a></sup></p> <p>In 1999, Khan won the <a href="/wiki/Filmfare_Award_for_Best_Supporting_Actor" title="Filmfare Award for Best Supporting Actor">Filmfare Award for Best Supporting Actor</a> for his extended guest appearance in <i><a href="/wiki/Kuch_Kuch_Hota_Hai" title="Kuch Kuch Hota Hai">Kuch Kuch Hota Hai</a></i> (1998). In 2011, he won the <a href="/wiki/Screen_Award_for_Best_Actor" title="Screen Award for Best Actor">Screen Award for Best Actor</a> for his performance in <i><a href="/wiki/Dabangg" title="Dabangg">Dabangg</a></i> and in 2013, he won the <a href="/wiki/Screen_Award_for_Best_Actor_(Popular_Choice)" title="Screen Award for Best Actor (Popular Choice)">Best Actor Popular Choice</a> for his performances in <i><a href="/wiki/Ek_Tha_Tiger" title="Ek Tha Tiger">Ek Tha Tiger</a></i> and <i><a href="/wiki/Dabangg_2" title="Dabangg 2">Dabangg 2</a></i>. Eight of the films he has acted in, have accumulated gross earnings of over <span class="nowrap"><a href="/wiki/Indian_rupee" title="Indian rupee"><img alt="INR" src="//upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Indian_Rupee_symbol.svg/7px-Indian_Rupee_symbol.svg.png" width="7" height="10" style="vertical-align: baseline" srcset="//upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Indian_Rupee_symbol.svg/11px-Indian_Rupee_symbol.svg.png 1.5x, //upload.wikimedia.org/wikipedia/commons/thumb/e/ee/Indian_Rupee_symbol.svg/14px-Indian_Rupee_symbol.svg.png 2x" /></a>1 billion</span> (US$15&#160;million) worldwide.<sup id="cite_ref-GROSS_5-0" class="reference"><a href="#cite_note-GROSS-5"><span>[</span>5<span>]</span></a></sup><sup id="cite_ref-world_6-0" class="reference"><a href="#cite_note-world-6"><span>[</span>6<span>]</span></a></sup> He played leading roles in five consecutive blockbusters namely <i><a href="/wiki/Dabangg" title="Dabangg">Dabangg</a></i>, <i><a href="/wiki/Ready_(2011_film)" title="Ready (2011 film)">Ready</a></i>, <i><a href="/wiki/Bodyguard_(Hindi_film)" title="Bodyguard (Hindi film)" class="mw-redirect">Bodyguard</a></i>, <i><a href="/wiki/Ek_Tha_Tiger" title="Ek Tha Tiger">Ek Tha Tiger</a></i>, and <i><a href="/wiki/Dabangg_2" title="Dabangg 2">Dabangg 2</a></i>. He has starred in more than 80 <a href="/wiki/Hindi_films" title="Hindi films" class="mw-redirect">Hindi films</a> and thus far<sup id="cite_ref-7" class="reference"><a href="#cite_note-7"><span>[</span>7<span>]</span></a></sup> has established himself as a leading actor of Hindi cinema.<sup id="cite_ref-8" class="reference"><a href="#cite_note-8"><span>[</span>8<span>]</span></a></sup><br />";  

来自维基百科的文本,我想从中删除所有锚标签。因为它在我的主机中提供了错误的链接,所以我想处理它。要么从其中删除所有锚标签,要么链接到维基百科。
给出错误:找不到路线 更好的例子:

href="/wiki/Dabangg_2" to href="http://en.wikipedia.org/wiki/Dabangg_2"

它在新标签中的所有锚标签上

【问题讨论】:

  • 简单的“javascript删除标签”谷歌搜索不会给你任何东西?
  • 如果我能在新标签中将它链接到维基百科会更好。我有锚标签的所有地址,但它在我的主机中重定向。
  • 所以修改那些标签指向维基百科..真的很简单..???并将 target="_blank" 添加到锚点。这一切都可以用简单的 javascript 完成。
  • 很容易喜欢 $('a#link').attr("href", "newadress"); 和 $('a#link').attr("target", "_blank" );

标签: javascript jquery html tags anchor


【解决方案1】:

这将从文本中删除所有链接,只留下其中的文本

$('#search a').contents().unwrap();

插入文字后在success函数中添加


更改维基百科的链接

$('#search a').attr('href', function(){
    if(this.href.search('/wiki/') > -1) {
        $(this).attr('target', '_newtab'); // opens a new tab on click
        return this.href.replace(location.hostname, 'www.wikipedia.org');
            // replaces the domain name with wikipedia in your link
    }
});

【讨论】:

  • 哇,我到处找这样的东西。
【解决方案2】:

您可以使用删除所有html标签

$("#search").html($("#search").text());

如果您只想删除带有 name 属性的 &lt;a&gt; 标记

$("#search a[name]").each(function(){

 $(this).prepend($(this).text());

}).remove();

【讨论】:

    【解决方案3】:

    javascript

    <script>
        $(document).ready(function () {
            $('.wrapper a').remove();
        });
    </script>
    

    html

    <div class="wrapper">
        <a href="/test/value?a=2">the link</a>
        fsdfhsd
        <a href="/test/value?a=2">the link</a>
    </div>
    

    【讨论】:

      猜你喜欢
      • 2014-07-02
      • 2010-12-06
      • 2017-02-05
      • 2011-10-08
      • 1970-01-01
      • 1970-01-01
      • 2022-08-09
      • 2013-12-10
      • 2022-06-30
      相关资源
      最近更新 更多