javascript - 解析 dom 元素时 JqueryUI slider 不工作

标签 javascript php jquery html jquery-ui

我正在使用简单的 HTML dom 解析器。 一切都适用于我的代码,但 jqueryui 无法正常工作,并且对于某些站点,它不显示图像。 请查看现场here 请尝试使用简单的 HTML dom parser .

请在带有 URL 的文本字段中输入 URL。 您可以看到图像未加载且 slider 不工作。 这是我的代码

<?php
include_once 'simple_html_dom.php';
$data = new simple_html_dom();

if ( isset($_REQUEST['url_name']) ) {
    if ( strpos($_REQUEST['url_name'], "http://") === false && strpos($_REQUEST['url_name'], "//") === false ) {
        $_REQUEST['url_name']="http://".$_REQUEST['url_name'];
    }

    $url_name = $_REQUEST['url_name'];
    if ( strpos($_REQUEST['url_name'], "/") === false ) {
        $url_name = $_REQUEST['url_name'].'/';
    }

    // Load HTML from an URL 
    $data->load_file($_REQUEST['url_name']);
    foreach ( $data->find('img') as $element ) {
        $element->target='_blank';
        if (    strpos($element, ".com") === false
             && strpos($element, ".net") === false
             && strpos($element, ".org") === false
             && strpos($element, "http://") === false
             && strpos($element, "https://") === false
           ){
            $element->src=$url_name.$element->src;
        }
    }

    foreach ( $data->find('style') as $element ) {
        if (    strpos($element, ".com") === false
             && strpos($element, ".net") === false
             && strpos($element, ".org") === false
             && strpos($element, "http://") === false
             && strpos($element, "https://") === false
           ){
            $element->src=$url_name.$element->src;
        }
    }

    foreach ( $data->find('script') as $element ) {
        if (    strpos($element, ".com") === false
             && strpos($element, ".net") === false
             && strpos($element, ".org") === false
             && strpos($element, "http://") === false
             && strpos($element, "https://") === false
           ){
            $element->src=$url_name.$element->src;
        }
    }

    foreach ( $data->find('link') as $element ) {
        if (     strpos($element, ".com") === false
             && strpos($element, ".net") === false
             && strpos($element, ".org") === false
             && strpos($element, "http://") === false
             && strpos($element, "https://") === false
           ){
            $element->href=$url_name.$element->href;
        }
    }

    foreach ( $data->find('a') as $element ) {
        if (    strpos($element->href, ".com") === false
             && strpos($element->href, ".net") === false
             && strpos($element->href, ".org") === false
             && strpos($element->href, "http://") === false
             && strpos($element->href, "https://") === false
           ){
            $element->href = "form_submit.php?url_name=".$url_name.$element->href;
        } else {
            $element->href = "form_submit.php?url_name=".$element->href;
        }
    }

    echo $newHtml;
}
?>

最佳答案

你想做什么?

我猜你想要一个包含所有托管 img、css、js 和 url 的列表。

  1. $newHtml 从未在您的代码中的任何地方定义。
  2. 您的simple_html_dom 类是什么?我假设是这样的:http://simplehtmldom.sourceforge.net/
    • 要么那样,要么您打算在最后echo $data;
  3. 函数!至少!

剩下的你得想办法

因为我真的不知道你要做什么,你的 simple_html_dom 实际上是什么,我所能做的就是向你介绍一些更高级的 编码概念。

  • 在代码样式中正确使用空格
    • 有些人会不同意我在这里的选择,但这会大大提高可读性。
  • 如果队伍真的很长,请将它们分开。
  • 如果您的代码中有重复的几乎相同的代码块,它可能应该是一个函数。
  • 调试/故障排除时,回显所有内容或使用调试器 (xdebug)。
  • 更好地注释你的代码,我没有在这里应该告诉你为什么你总是应该。

调整后的代码:

<?php
include_once 'simple_html_dom.php';
$data = new simple_html_dom();

if ( isset($_REQUEST['url_name']) ) {
    if ( strpos($_REQUEST['url_name'], "http://") === false && strpos($_REQUEST['url_name'], "//") === false ) {
        $_REQUEST['url_name']="http://".$_REQUEST['url_name'];
    }

    $data->my_url_name = $_REQUEST['url_name'];
    if ( strpos($_REQUEST['url_name'], "/") === false ) {
        $data->my_url_name = $_REQUEST['url_name'].'/';
    }

    echo "<div style='border:1px solid blue;'>";

        // Load HTML from an URL
        $data->load_file($_REQUEST['url_name']);

        modifyUrls($data, 'img', array('target' => '_blank'));
        modifyUrls($data, 'style');
        modifyUrls($data, 'script');
        modifyUrls($data, 'link', array(), 'href');

        foreach ( $data->find('a') as $element ) {
            if ( checkurl($element->href) ) {
                echo $element->href = "form_submit.php?url_name=". $data->my_url_name . $element->href;
            } else {
                echo $element->href = "form_submit.php?url_name=". $element->href;
            }
            echo "<br>";
        }

        echo "</div><div style='border:1px solid red;'>";
        echo $data;
        echo "</div>";
    }

function modifyUrls(&$dataObj, $target, $options = array(), $urlAttribute = "src") {
    foreach ( $dataObj->find($target) as $element ) {
        if ( !empty($options) ) {
            foreach ($options as $field => $value) {
                $element->{$field} = $value;
            }
        }

        if ( checkurl($element) ) {
            echo $element->{$urlAttribute} = $dataObj->my_url_name . $element->{$urlAttribute};
            echo "<br>";
        }
    }
}

function checkurl($url){
    if (    strpos($url, ".com") === false
        && strpos($url, ".net") === false
        && strpos($url, ".org") === false
        && strpos($url, "http://") === false
        && strpos($url, "https://") === false
    ){
        return true;
    }

    return false;
}

关于javascript - 解析 dom 元素时 JqueryUI slider 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34350008/

相关文章:

javascript - 从 Reddit 的 API 中检索评论

javascript - 包含按钮错误 : I would a button every 8 elements of the list 的列表

javascript - 使用 javascript 原型(prototype)在链接点击后保留数据

javascript - 如何以非 Ajax 方式使用 jQuery(或 Javascript)从 API 调用中获取 JSON 结果?

javascript - 无法从服务器端获取数组

javascript - 在应用程序加载之前 ReactJS auth 重定向

php - 为什么在编译 PHP 扩展 DLL 时会出现 LNK2005 错误

php - 提高(查询)性能

php - ajax 接收到的字符串与从 php 发送的字符串不同

Javascript:回调注入(inject)替代方案?