PHP foreach、CSS和jQuery脚本。。如何互动?
我试图在PHP文件上使用jQuery显示/隐藏脚本,它是这样的:PHP foreach、CSS和jQuery脚本。。如何互动?,php,jquery,css,html,foreach,Php,Jquery,Css,Html,Foreach,我试图在PHP文件上使用jQuery显示/隐藏脚本,它是这样的: <?php foreach ($empresas as $empresa) { echo "<style type='text/css'> .$empresa[teaserid] { width:100%; background-color: #CCC; color: #000; margin-top:10px; border:1px solid #CCC; position:relative; vertica
<?php
foreach ($empresas as $empresa) {
echo "<style type='text/css'>
.$empresa[teaserid] {
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
.showhide$empresa[teaserid] {
display:none;
}
</style>";
echo '<script type="text/javascript">';
echo ' $(document).ready(function(){ ';
echo ' $(\".$empresa[teaserid]\").hide(); ';
echo ' $(\".showhide$empresa[teaserid]\").show(); ';
echo ' $(\".showhide$empresa[teaserid]\").click(function(){ ';
echo ' $(\".$empresa[teaserid]\").slideToggle(), 500; ';
echo ' }); ';
echo ' });';
echo '</script>';
echo "<a href='#' class='showhide$empresa[teaserid]'>$empresa[titulo]</a><br />
<div class='$empresa[teaserid]'>
TEST</div>";
}
?>
<?php
$variable = "SOme value";
echo "Some very long huge string with <html> tags and stuff plus $variables";
?>
在PHP中,只能将变量放在双引号字符串中:
$a = 'Jon';
echo "Hi $a"; // Should output 'Hi Jon'
echo 'Hi $a'; // should output 'Hi $a'
因此,如果希望Javascript读取:
$(".Jon") // where Jon is the value of $a
然后在PHP中,您可以这样输出:
echo '$(\".' . $a . '\")'; // Notice we are splitting the echo statement into a concatenation of 3 strings
在PHP中,只能将变量放在双引号字符串中:
$a = 'Jon';
echo "Hi $a"; // Should output 'Hi Jon'
echo 'Hi $a'; // should output 'Hi $a'
因此,如果希望Javascript读取:
$(".Jon") // where Jon is the value of $a
然后在PHP中,您可以这样输出:
echo '$(\".' . $a . '\")'; // Notice we are splitting the echo statement into a concatenation of 3 strings
实际上不需要让PHP为每个项目打印一个新的CSS和Javascript块。如果您编写css和javascript是一种好方法,那么它将为您处理
要记住的另一件事是,PHP是最基本的模板语言,所以如果您发现自己在回显大量代码,可能是出了问题。而是像这样:
<?php
foreach ($empresas as $empresa) {
echo "<style type='text/css'>
.$empresa[teaserid] {
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
.showhide$empresa[teaserid] {
display:none;
}
</style>";
echo '<script type="text/javascript">';
echo ' $(document).ready(function(){ ';
echo ' $(\".$empresa[teaserid]\").hide(); ';
echo ' $(\".showhide$empresa[teaserid]\").show(); ';
echo ' $(\".showhide$empresa[teaserid]\").click(function(){ ';
echo ' $(\".$empresa[teaserid]\").slideToggle(), 500; ';
echo ' }); ';
echo ' });';
echo '</script>';
echo "<a href='#' class='showhide$empresa[teaserid]'>$empresa[titulo]</a><br />
<div class='$empresa[teaserid]'>
TEST</div>";
}
?>
<?php
$variable = "SOme value";
echo "Some very long huge string with <html> tags and stuff plus $variables";
?>
您可以这样做,多次打开和关闭标记
<?php $variable = "SOme value"; ?>
Some very long huge string with <html> tags and stuff plus <?php print $variables; ?>
一些非常长的巨大的字符串,带有标签和其他东西
至此,这里是您的代码的一个修改版本。
同样,您不需要让PHP输出带有硬编码ID的定制Javascript,而真正需要的是构建您的Javascript,以便它不需要像我所做的那样了解任何这些内容
<style type='text/css'>
/* The CSS is the same, so just have one class that is used on all the items. */
.empresa-teaser {
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
</style>
<script type="text/javascript">
$(function(){
// Hide all of the teasers to start.
$(".empresa-teaser").hide();
// Add a handler for when the show/hide link is clicked.
$(".showhide-empresa-teaser").click(function(){
// When it is clicked, find the next item with the 'empresa-teaser' class
// and show it using a sliding toggle effect.
$(this).nextAll('.empresa-teaser:first').slideToggle(500);
});
});
</script>
<?php foreach ($empresas as $empresa) { ?>
<a href='#' class="showhide-empresa-teaser">
<?php print $empresa['titulo']; ?>
</a>
<br />
<div class="empresa-teaser">TEST</div>
<?php } ?>
/*CSS是相同的,所以只有一个类用于所有项目*/
empresa Triser{
宽度:100%;
背景色:#CCC;
颜色:#000;
边缘顶部:10px;
边框:1px实心#CCC;
位置:相对位置;
垂直对齐:中间对齐;
}
$(函数(){
//隐藏所有的挑逗开始。
$(“.empresa trister”).hide();
//为单击“显示/隐藏”链接时添加处理程序。
$(“.showhide empresa摘要”)。单击(函数(){
//单击后,使用“empresa摘要”类查找下一个项目
//并使用滑动切换效果显示它。
$(this).nextAll('.empresa-trister:first').slideToggle(500);
});
});
试验
实际上不需要让PHP为每个项目打印新的CSS和Javascript块。如果您编写css和javascript是一种好方法,那么它将为您处理
要记住的另一件事是,PHP是最基本的模板语言,所以如果您发现自己在回显大量代码,可能是出了问题。而是像这样:
<?php
foreach ($empresas as $empresa) {
echo "<style type='text/css'>
.$empresa[teaserid] {
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
.showhide$empresa[teaserid] {
display:none;
}
</style>";
echo '<script type="text/javascript">';
echo ' $(document).ready(function(){ ';
echo ' $(\".$empresa[teaserid]\").hide(); ';
echo ' $(\".showhide$empresa[teaserid]\").show(); ';
echo ' $(\".showhide$empresa[teaserid]\").click(function(){ ';
echo ' $(\".$empresa[teaserid]\").slideToggle(), 500; ';
echo ' }); ';
echo ' });';
echo '</script>';
echo "<a href='#' class='showhide$empresa[teaserid]'>$empresa[titulo]</a><br />
<div class='$empresa[teaserid]'>
TEST</div>";
}
?>
<?php
$variable = "SOme value";
echo "Some very long huge string with <html> tags and stuff plus $variables";
?>
您可以这样做,多次打开和关闭标记
<?php $variable = "SOme value"; ?>
Some very long huge string with <html> tags and stuff plus <?php print $variables; ?>
一些非常长的巨大的字符串,带有标签和其他东西
至此,这里是您的代码的一个修改版本。
同样,您不需要让PHP输出带有硬编码ID的定制Javascript,而真正需要的是构建您的Javascript,以便它不需要像我所做的那样了解任何这些内容
<style type='text/css'>
/* The CSS is the same, so just have one class that is used on all the items. */
.empresa-teaser {
width:100%;
background-color: #CCC;
color: #000;
margin-top:10px;
border:1px solid #CCC;
position:relative;
vertical-align:middle;
}
</style>
<script type="text/javascript">
$(function(){
// Hide all of the teasers to start.
$(".empresa-teaser").hide();
// Add a handler for when the show/hide link is clicked.
$(".showhide-empresa-teaser").click(function(){
// When it is clicked, find the next item with the 'empresa-teaser' class
// and show it using a sliding toggle effect.
$(this).nextAll('.empresa-teaser:first').slideToggle(500);
});
});
</script>
<?php foreach ($empresas as $empresa) { ?>
<a href='#' class="showhide-empresa-teaser">
<?php print $empresa['titulo']; ?>
</a>
<br />
<div class="empresa-teaser">TEST</div>
<?php } ?>
/*CSS是相同的,所以只有一个类用于所有项目*/
empresa Triser{
宽度:100%;
背景色:#CCC;
颜色:#000;
边缘顶部:10px;
边框:1px实心#CCC;
位置:相对位置;
垂直对齐:中间对齐;
}
$(函数(){
//隐藏所有的挑逗开始。
$(“.empresa trister”).hide();
//为单击“显示/隐藏”链接时添加处理程序。
$(“.showhide empresa摘要”)。单击(函数(){
//单击后,使用“empresa摘要”类查找下一个项目
//并使用滑动切换效果显示它。
$(this).nextAll('.empresa-trister:first').slideToggle(500);
});
});
试验
值得一提的是:
您可以创建一个设置对象来保存变量。例如:
您的php:
<?php
$settings = array( 'settingA' => 'something',
'settingB' => 'something else',
'wat' => 9001 );
?>
因此,您可以将所需的服务器信息放在一个全局对象中(或者将其作为应用程序对象或其他对象的属性),并可以访问它
console.log( settings.settingA ); // returns "something"
进一步看看你的问题,别忘了你不必停留在PHP标签中。我并不提倡在css中使用PHP,但这应该有助于您理解以下概念:
<style>
<?php foreach ($empresas as $empresa): ?>
.showhide<?php echo $empresa['teaserid'] ?> {
display:none;
}
<?php endforeach; ?>
</style>
.展示皮{
显示:无;
}
值得一提的是:
您可以创建一个设置对象来保存变量。例如:
您的php:
<?php
$settings = array( 'settingA' => 'something',
'settingB' => 'something else',
'wat' => 9001 );
?>
因此,您可以将所需的服务器信息放在一个全局对象中(或者将其作为应用程序对象或其他对象的属性),并可以访问它
console.log( settings.settingA ); // returns "something"
进一步看看你的问题,别忘了你不必停留在PHP标签中。我并不提倡在css中使用PHP,但这应该有助于您理解以下概念:
<style>
<?php foreach ($empresas as $empresa): ?>
.showhide<?php echo $empresa['teaserid'] ?> {
display:none;
}
<?php endforeach; ?>
</style>
.展示皮{
显示:无;
}
我不是程序员(正如你可能看到的……没关系,我们都从某个地方开始……读这篇文章……灯泡将打开,然后这个谜题有两个独立的部分,这就是为什么它被称为“客户机-服务器”关系。两者不会以您认为的方式进行交互。PHP生成HTML。HTML会传递给浏览器。用户做一些事情,一些数据会传递回PHP文件,然后由PHP文件决定为用户的下一页生成哪个HTML。我不是程序员(正如你可能看到的……没关系,我们都从某个地方开始……读这篇文章……灯泡将打开,然后这个谜题有两个独立的部分,这就是为什么它被称为“客户机-服务器”关系。两者不会以您认为的方式进行交互。PHP生成HTML。HTML被传递到浏览器。用户做一些事情,一些数据被传递回PHP文件,然后PHP文件决定为用户的下一页生成哪个HTML。PHP中的var语句如何处理ya?PHP中的var语句如何处理ya?这些所有的事情。所有的事情。做所有的事情。所有的事情。做所有的事情。哇!这很漂亮!:)谢谢你洛根!哇!这很漂亮!:)谢谢你洛根!