0

我的 jquery 代码有这个问题:

<?php

//I oppen a text document, read the text inside of it and write it inside my html page.
//When someone clicks on a line, I want to take that very same line and send it via  select(String) function. 
$handle = fopen($_POST['lien'], 'r');

if ($handle)
{       
    while (!feof($handle))
    {       
          $buffer = fgets($handle);     
          echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";
             //It works when I put a simple string within the select param: 
             //echo "<div onclick='select(\" text \");'>".$buffer."</div><br/>";
    }
    fclose($handle);
}
?>

jQuery代码:

function select(text){
        alert(text);
        //$("#selected").html();
    }

你们认为问题出在哪里?

谢谢 :)

4

2 回答 2

1

也许您的 $buffer 包含一些双引号,然后会干扰周围的双引号。如果是这种情况,请查看生成的 HTML 代码,也许您会看到如下内容:

<div onclick='select("He said: "Freeze!"")'>

... Javascript 无法正确解析。如果是这种情况,请考虑使用:

echo "<div onclick='select(".json_encode($buffer).")'>";
于 2013-11-10T21:00:36.063 回答
0

尝试换行:

    echo "<div onclick='select(\" ".$buffer." \");'>".$buffer."</div><br/>";

    echo "<div onclick='select(\" ".rtrim($buffer)." \");'>".$buffer."</div><br/>";

或者:

    echo "<div onclick='select($(this).text());'>".$buffer."</div><br/>";
于 2013-11-10T20:51:08.417 回答