【问题标题】:Include only a small part of another script with javascript使用 javascript 仅包含另一个脚本的一小部分
【发布时间】:2017-03-18 17:03:53
【问题描述】:

有没有一种方法可以让我只包含这样的脚本的一小部分:

<?php
$url = 'https://www.example.com';
$content = file_get_contents($url);
$first_step = explode( '<div class="HelloWorld">' , $content );
$second_step = explode('</div class="HelloWorld">' , $first_step[1] );

echo $second_step[0];

...但是只使用 Javascript 而不是 PHP?

谢谢

【问题讨论】:

  • 使用 jquery.fn.html 方法 $('.Hellwold').html()

标签: javascript php include


【解决方案1】:

我认为您想要的是获取 div helloworld 的 innerHTML。改为使用 jquery.fn.html

console.log($(".HelloWorld").html());
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="HelloWorld"><span>foo</span>bar</div>

或者您可以直接使用 HTMLElement 具有 innerHTML 属性的 DOM api 来完成:

console.log(document.querySelector(".HelloWorld").innerHTML);
 &lt;div class="HelloWorld"&gt;&lt;span&gt;foo&lt;/span&gt;bar&lt;/div&gt;

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-02-26
    • 2015-03-01
    • 2021-10-24
    相关资源
    最近更新 更多