繁体   English   中英

使用jquery load()从外部页面获取h1的内容

[英]Get content of h1 from external page with jquery load()

我有一个mediawiki,我想将内容从另一个页面获取。 所以我有: http : //bourlo.net/wiki/index.php/Lunet

并希望在另一页上的引导模式中显示部分内容: http//bourlo.net/stack/

Wiki页面的标题可通过以下方式检索:

  $("#wikiModal h4.modal-title")
    .load( "http://bourlo.net/wiki/index.php/Lunet .firstHeading");

那行得通,是的! 但是我不想要完整的

<h1 id="firstHeading" class="firstHeading" lang="nl-informal">Lunet</h1>

<h4>从模式,但只有内容>>> Lunet

我怎样才能做到这一点?

您需要改用其他ajax方法。 例如:

$.get("http://bourlo.net/wiki/index.php/Lunet", function(html){
    var txt = $(html).find('.firstHeading').text();
    $("#wikiModal h4.modal-title").text(txt);
});

因此,您只想从ajax返回的文本中提取文本:

$.get( "http://bourlo.net/wiki/index.php/Lunet", function(html){

     $("#wikiModal h4.modal-title").text( $(html).find('.firstHeading').text() );
});

这是因为使用.load() ,无法在插入DOM之前操纵responseText 让我们确认您实际上可以执行以下操作:

$h4 = $("#wikiModal h4.modal-title")
$h4
   .load( "http://bourlo.net/wiki/index.php/Lunet #firstHeading", function(){
        $h4.find('#firstHeading').replaceWith(function(){
            return $(this).text();
        });
    });

这绝对比较笨拙。 但是我不愿意这样做,因为.load您会受到无法控制的因素的限制而不得不使用.load版本而不是.get版本。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM