Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/464.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/86.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 使用jquery load()从外部页面获取h1的内容_Javascript_Jquery_Twitter Bootstrap - Fatal编程技术网

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

Javascript 使用jquery load()从外部页面获取h1的内容,javascript,jquery,twitter-bootstrap,Javascript,Jquery,Twitter Bootstrap,我有一个mediawiki,我想从中获取另一个页面的内容。因此,我: 并希望在另一个页面上以引导模式显示其中的部分内容: 通过以下方式检索wiki页面的标题: $("#wikiModal h4.modal-title") .load( "http://bourlo.net/wiki/index.php/Lunet .firstHeading"); 行得通,是的!但我不想要完整的 <h1 id="firstHeading" class="firstHeading" lang

我有一个mediawiki,我想从中获取另一个页面的内容。因此,我:

并希望在另一个页面上以引导模式显示其中的部分内容:

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

  $("#wikiModal h4.modal-title")
    .load( "http://bourlo.net/wiki/index.php/Lunet .firstHeading");
行得通,是的!但我不想要完整的

<h1 id="firstHeading" class="firstHeading" lang="nl-informal">Lunet</h1>
Lunet
在模式的
中,但仅在内容>>>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){
    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
版本而不是
。通过超出你控制的因素获取
版本。

所以你只想从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(“http://bourlo.net/wiki/index.php/Lunet .firstHeading>*”
@Moob
Lunet
是一个文本节点,不能使用CSS/jQuery将其作为目标selector@A.Wolff你说得对。我的错;)<代码>…加载(“http://bourlo.net/wiki/index.php/Lunet .firstHeading>*”
@Moob
Lunet
是一个文本节点,不能使用CSS/jQuery将其作为目标selector@A.Wolff你说得对。我的错;)你指的是什么?
function(html)
这里就是
http://bourlo.net/wiki/index.php/Lunet
code-pageWell,这不是获取远程页面的唯一方法吗?如果您想要一个页面部分,那么只有
.load
可以这样做。是的,因此您可以加载整个页面并开始DOM处理!顺便说一句,在任何情况下,我越看我的答案,我就越觉得它和你的答案基本上是一样的。不是吗?您的代码应该是:
$(“#wikiModal h4.modal title”).text($(html).find('.firstHeading').text())然后它将按照OP的预期工作,然后它将与我的答案相同。在您的代码中,您不仅添加了
.firstHeading
元素的文本,还添加了整个代码页(解析后减去了html、head、body等元素)。我在第二个按钮中添加了您的建议,请参见区别:您指的是什么?
函数(html)
这里就是
http://bourlo.net/wiki/index.php/Lunet
code-pageWell,这不是获取远程页面的唯一方法吗?如果您想要一个页面部分,那么只有
.load
可以这样做。是的,因此您可以加载整个页面并开始DOM处理!顺便说一句,在任何情况下,我越看我的答案,我就越觉得它和你的答案基本上是一样的。不是吗?您的代码应该是:
$(“#wikiModal h4.modal title”).text($(html).find('.firstHeading').text())然后它将按照OP的预期工作,然后它将与我的答案相同。在您的代码中,您不仅添加了
.firstHeading
元素的文本,还添加了整个代码页(解析后减去了html、head、body等元素),我在第二个按钮中添加了您的建议,请参见区别: