如何打开在div中解析的链接?

如何打开在div中解析的链接?

问题描述:

我的网站解析西班牙语字典,并允许您一次搜索多个单词。如果你看到“hola”,你会得到第一个div)。有些话拿出建议,如 “卡萨”,而不是像 “HOLA” 的定义:如何打开在div中解析的链接?

enter image description here

什么我期待的是这样的:

enter image description here

所以,我希望:当您点击建议(例如我发布的示例中的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/>';但它没有工作,也不知道真的哪里/如何使用它。

+1

首先,+1纯粹是因为我喜欢这个概念。其次,也许你应该研究javascript或jQuery以及ajax这种交互式用户体验。 – 2013-04-10 00:35:08

+0

@ francisco.preller感谢弗朗西斯科,但我只是开始编程ahaha。我不知道从哪里开始解决这个问题,所以我期待着直接的回应或者是一个例子。谢谢! – 2013-04-10 00:41:21

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

注:我不建议使用jQuery -BEFORE-学习JavaScript - DO IT AT自负,但至少BACK快来学习JavaScript LATER

首先,如何下载阅读起来安装jquery here。其次,你会想要一些这样的东西,让我们假装这是你的标记。

<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> 

希望评论能够说明一些问题,然后您需要设置您的php脚本,它将发送一个GET请求。 (例如,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; 
?> 

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

+0

弗朗西斯科杰出的解释,但问题不是专门用“casa”这个词,而是用任何可能有建议的词。 – 2013-04-10 02:09:45

+0

事实上,我发布的代码工作原理如下:在西班牙语这里添加任何单词:但唯一的问题是像“casa”这样的词,你真的认为我必须从头开始用你的代码从头开始吗? – 2013-04-10 02:11:58

+1

很高兴你在工作,在真实世界的情况下,而不是我的例子,你会用锚标签的文本替代单词'casa'或其他东西。例如$(“suggestions a”)。text()而不是ajax中的静态值'casa'。 – 2013-04-10 02:14:46