【问题标题】:adding content from search result with jquery使用 jquery 从搜索结果中添加内容
【发布时间】:2013-01-21 06:56:36
【问题描述】:

我正在使用这个很酷的脚本http://qpoit.com/marcofolio_demo/apple_search/,但是当我单击搜索结果时,我希望将其作为 html 内容添加到我将使用的 id 中。我知道这是用 $("#displayContent").html(data);但是我需要我点击的搜索结果的内容,而不是找到的所有结果。请帮忙

【问题讨论】:

    标签: html ajax jquery


    【解决方案1】:

    搜索的 rpc.php 文件包含生成搜索输出的函数。 在第 29 行,将内容更改为 JavaScript 函数,它会为您工作。

    // Used in line 29 in rpc.php
    echo '<a href="'.$result->url.'">';
    
    // Change it to something like
    echo '<a onclick="applyContent('.$result->url.')">'
    

    之后,返回 index.html(或您正在使用 apple-search 的文件),确保 jQuery 已加载并添加如下内容:

    <script type="text/javascript">
    // Use the same function name, as in the php-file
    function applyContent (resultURL) {
        // Use jQuerys get-function to get the content of the file you've found in the search
        $.get(resultURL, function(data) {
            // Change '#finalResult' to the css-selector, of the element, which should contain the result at the end
            $('#finalResult').html(data);
        });
    }
    </script>
    

    单击搜索结果后,它应该使其成为#finalResult 的内容,或您选择的任何元素。

    【讨论】:

    • 太棒了!但是我不希望找到我找到的文件的内容,我只想将 rpc.php 返回的 html 数据添加到#finaleResult。我怎么做?我知道脚本会这样做,但它会消失,我希望将点击的结果附加到#finalResult
    • 哦,等等,我明白了。当您提出新问题时,我想要与 stackoverflow 用于添加关键字的完全相同的东西。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2014-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多