PHP echo中的Jquery
这是我的php和jquery代码:PHP echo中的Jquery,php,jquery,Php,Jquery,这是我的php和jquery代码: <?php require('connection.php'); $query="select title,content from blogs"; echo '<html><head>'; echo '<link rel="stylesheet" href="blog.css" />'; echo '<script src="jquery.js"></script>'; //echo '<
<?php
require('connection.php');
$query="select title,content from blogs";
echo '<html><head>';
echo '<link rel="stylesheet" href="blog.css" />';
echo '<script src="jquery.js"></script>';
//echo '<script src="blogs.js"></script>';
echo "</head><body>";
$i=0;
if($result=$mysqli->query($query))
{
while($news=$result->fetch_row())
{
echo "<br/><br />"."<strong ><h2 onclick='$(document).ready(function() {
$(this).click(function(){ $(\"#a".$i."\").toggle(\"slow\");});});'>". $news[0]."</h2></strong>"."<br/><br />";
echo "<div id='a".$i."'>".$news[1]."</div>";
$i++;
}
$result->close();
echo "</body></html>";
}
?>
以下几点就足够了,也许可以摆脱你的意外行为
<h2 onclick='$("#a0").toggle("slow")'>hello world</h2>
你好,世界
此外,您通常会在javascript文件或javascript标记中执行此操作。在h2上放置一个ID,并将侦听器添加到该h2中
此外,一个好的做法是使用css文件将粗体应用于h2元素,而不是在HTML中加上粗体。以下内容就足够了,可能会消除您的意外行为
<h2 onclick='$("#a0").toggle("slow")'>hello world</h2>
你好,世界
此外,您通常会在javascript文件或javascript标记中执行此操作。在h2上放置一个ID,并将侦听器添加到该h2中
另外,一个好的做法是使用css文件将粗体应用于h2元素,而不是在HTML周围加上粗体
<?php
require('connection.php');
$query="select title,content from blogs";
echo '<html><head>';
echo '<link rel="stylesheet" href="blog.css" />';
echo '<script src="jquery.js"></script>';
echo '<script type="text/javascript">';
echo ' $(document).ready(function() {';
echo ' $("h2").click(function(){ ;$('#a' + $(this).data("id")).toggle("slow");});});';
echo '</script>';
//echo '<script src="blogs.js"></script>';
echo "</head><body>";
$i=0;
if($result=$mysqli->query($query))
{
while($news=$result->fetch_row())
{
echo "<br/><br />"."<strong ><h2 data-id=".$i.">". $news[0]."</h2></strong>"."<br/><br />";
echo "<div id='a".$i."'>".$news[1]."</div>";
$i++;
}
$result->close();
echo "</body></html>";
}
?>
我最好这样做:
<?php
require('connection.php');
$query="select title,content from blogs";
echo '<html><head>';
echo '<link rel="stylesheet" href="blog.css" />';
echo '<script src="jquery.js"></script>';
echo '<script type="text/javascript">';
echo ' $(document).ready(function() {';
echo ' $("h2").click(function(){ ;$('#a' + $(this).data("id")).toggle("slow");});});';
echo '</script>';
//echo '<script src="blogs.js"></script>';
echo "</head><body>";
$i=0;
if($result=$mysqli->query($query))
{
while($news=$result->fetch_row())
{
echo "<br/><br />"."<strong ><h2 data-id=".$i.">". $news[0]."</h2></strong>"."<br/><br />";
echo "<div id='a".$i."'>".$news[1]."</div>";
$i++;
}
$result->close();
echo "</body></html>";
}
?>
以下是您的清理代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="blog.css" />
<script src="jquery.js"></script>
<!--<script src="blogs.js"></script>-->
<script>
$(function(){
$('h2').click(function(){
$(this).next().toggle('slow')
})
})
</script>
</head>
<body>';
<?php
require('connection.php');
$query="select title,content from blogs";
$i=0;
if($result=$mysqli->query($query)) {
while($news=$result->fetch_row()) {
echo "
<h2><strong>{$news[0]}<strong></h2>
<div id='a$i'>{$news[1]}</div>";
$i++;
}
$result->close();
}
?>
</body>
</html>
$(函数(){
$('h2')。单击(函数(){
$(this.next().toggle('slow'))
})
})
';
以下是您的清理代码:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="blog.css" />
<script src="jquery.js"></script>
<!--<script src="blogs.js"></script>-->
<script>
$(function(){
$('h2').click(function(){
$(this).next().toggle('slow')
})
})
</script>
</head>
<body>';
<?php
require('connection.php');
$query="select title,content from blogs";
$i=0;
if($result=$mysqli->query($query)) {
while($news=$result->fetch_row()) {
echo "
<h2><strong>{$news[0]}<strong></h2>
<div id='a$i'>{$news[1]}</div>";
$i++;
}
$result->close();
}
?>
</body>
</html>
$(函数(){
$('h2')。单击(函数(){
$(this.next().toggle('slow'))
})
})
';
如果您坚持使用内联脚本,请不要使用$(document).ready
,它不是必需的。我删除了它,但仍然遇到相同的问题。这就是为什么它不是答案;)如果您坚持使用内联脚本,请不要使用$(document).ready
,它不是必需的。我删除了它,但仍然遇到相同的问题。这就是为什么它不是答案;)你不明白我的问题。我希望div id递增。我明白了,我只是更正了你输出的javascript。本可以用echo来完成,但更正就在那里。你不明白我的问题。我希望div id递增。我明白了,我只是更正了你输出的javascript。可以用回音来代替,但有更正。这个'h2'必须改为'h2',在headscript中的'slow'=>'slow\'也是一样。此外,如果你把所有html放在php blocl之外,它会更干净。至少有head和开始/结束body和head标记。这个'h2'必须更改为'h2\',在head script中的'slow'=>\'slow\'也必须更改为'h2\'。此外,如果将所有html放在php blocl之外,它会更干净。至少有头部和打开/关闭主体以及头部标签。