我的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();
}
你们认为这是问题所在?
谢谢:)
答案 0 :(得分:1)
也许你的$ buffer包含一些双引号,然后干扰周围的双引号。如果是这种情况,请查看生成的HTML代码,也许您会看到类似这样的内容:
<div onclick='select("He said: "Freeze!"")'>
...哪些Javascript无法正确解析。如果是这种情况,请考虑使用:
echo "<div onclick='select(".json_encode($buffer).")'>";
答案 1 :(得分: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/>";