1

我的网站解析一本西班牙语词典,让您一次搜索多个单词。如果你查找“hola”,你会得到第一个 div)。有些词提出了建议,例如“casa”,而不是“hola”之类的定义:

在此处输入图像描述

我正在寻找的是:

在此处输入图像描述

所以,我想:当您单击建议时(例如我发布的示例中的 CASAR)以将结果打印在像 HOLA 这样的 div 中。这是使用的代码:

$words = array('word0','word-1');
    function url_decode($string){
    return urldecode(utf8_decode($string));
    }

    $baseUrl = 'http://lema.rae.es/drae/srv/search?val=';

    $cssReplace = <<<EOT

    <style type="text/css">
    // I changed the style
    </style>
    </head>
 EOT;

 $resultIndex = 0;

foreach($words as $word) {
if(!isset($_REQUEST[$word]))
    continue;

$contents = file_get_contents($baseUrl . urldecode(utf8_decode($_REQUEST[$word])));

$contents = str_replace('</head>', $cssReplace, $contents);
$contents = preg_replace('/(search?[\d\w]+)/','http://lema.rae.es/drae/srv/search', $contents);


echo "<div style='
      //style
     ", (++$resultIndex) ,"'>", $contents,
        "</div>";
} 

我试过:$contents .= '<a href="">' . $word . '</a><br/>';但它没有用,我也不知道在哪里/如何使用它。

4

1 回答 1

1

好的,我将使用 jQuery 作为示例,因为如果您是编程新手,这将是最容易完成这项工作的。

注意:我不建议在学习 JAVASCRIPT 之前使用 JQUERY - 风险自负,但至少稍后再回来学习 JAVASCRIPT

首先,阅读如何在此处下载和安装 jquery 。

其次,你会想要这样的东西,让我们假设这是你的标记。

<div id="wrapper">
    <!-- This output div will contain the output of whatever the MAIN
         word being displayed is, this is where HOLA would be from your first example -->
    <div id="output">

    </div>

    <!-- This is your suggestions box, assuming all anchor tags in here will result in
         a new word being displayed in output -->
    <div id="suggestions">

    </div>
</div>

<!-- Le javascript -->
<script>
    // Standard jQuery stuff, read about it on the jquery documentation
    $(function() {
        // The below line is a selector, it will select any anchor tag inside the div with 'suggestions' as identifier
        $('#suggestions a').click(function(e) {
            // First, stop the link going anywhere
            e.preventDefault();

            // Secondly, we want to replace the content from output with new content, we will use AJAX here
            // which you should also read about, basically we set a php page, and send a request to it
            // and display the result without refreshing your page
            $.ajax({
                url: 'phppage.php',
                data: { word: 'casar' },
                success: function(output) {
                    // This code here will replace the contents of the output div with the output we brought back from your php page
                    $('#output').html(output);
                }
            })
        });
    })
</script>

希望评论会有所启发,然后您需要设置将发送 GET 请求的 php 脚本。(例如http://some.address.com/phppage.php/?word=casar

然后你只需回显 PHP 的输出

<?php

    $word = $_GET['word'];
    // Connect to some database, get the definitions, and store the results
    $result = someDatabaseFunctionThatDoesSomething($word);
    echo $result;
?>

希望这会有所帮助,我希望你有很多阅读要做!

于 2013-04-10T00:55:17.333 回答