【发布时间】:2012-02-18 14:36:11
【问题描述】:
我正在尝试将值从一个 div 传递给另一个。第一个 div 由 for each 循环生成,并且有一个条件语句,如果条件匹配,则将值传递给第二个 div,第二个 div 通过 jquery 弹出窗口显示。 这是我的代码:
<?php foreach ($value as $data) {
?>
<div class="center">
<h4>Description:</h4>
<?php
if(strlen($data['description'])<50)
{
echo $data['description'];
}
else
{
$str=$data['description'];
$substr=substr($str,0,50);
echo $substr.'<p class=button><a href=#>read more...<a/><p/>';
?>
<?php }?>
</div>
<?php }?>
<div class="popupContact">
<a class="popupContactClose">x</a>
<h1>Description</h1>
<p class="contactArea">
<?php echo $str;?>
</p>
</div>
<div class="backgroundPopup"></div>
这是我的 jquery 弹出窗口
//SETTING UP OUR POPUP
//0 means disabled; 1 means enabled;
var popupStatus = 0;
//loading popup with jQuery magic!
function loadPopup(){
//loads popup only if it is disabled
if(popupStatus==0){
$(".backgroundPopup").css({
"opacity": "0.7"
});
$(".backgroundPopup").fadeIn("slow");
$(".popupContact").fadeIn("slow");
popupStatus = 1;
}
}
//disabling popup with jQuery magic!
function disablePopup(){
//disables popup only if it is enabled
if(popupStatus==1){
$(".backgroundPopup").fadeOut("slow");
$(".popupContact").fadeOut("slow");
popupStatus = 0;
}
}
//centering popup
function centerPopup(){
//request data for centering
var windowWidth = document.documentElement.clientWidth;
var windowHeight = document.documentElement.clientHeight;
var popupHeight = $(".popupContact").height();
var popupWidth = $(".popupContact").width();
//centering
$(".popupContact").css({
"position": "absolute",
"top": windowHeight/2-popupHeight/3.25,
"left": windowWidth/2-popupWidth/2
});
//only need force for IE6
$(".backgroundPopup").css({
"height": windowHeight
});
}
//CONTROLLING EVENTS IN jQuery
$(document).ready(function(){
//LOADING POPUP
//Click the button event!
$(".button").click(function(){
//centering with css
centerPopup();
//load popup
loadPopup();
});
//CLOSING POPUP
//Click the x event!
$(".popupContactClose").click(function(){
disablePopup();
});
//Click out event!
$(".backgroundPopup").click(function(){
disablePopup();
});
//Press Escape event!
$(document).keypress(function(e){
if(e.keyCode==27 && popupStatus==1){
disablePopup();
}
});
});
现在,由于第一个 div 是从 for each 循环中生成的,我需要区分 read more.. 链接以确定单击了哪个 read more 并传递适当的 $str 到第二个 div。是否可以通过 jquery 传递值?或者有什么更好的方法吗?
这可能听起来令人困惑,因为我对我的话很虚弱,但如果有人明白我在说什么,请帮忙。
【问题讨论】:
-
您不应该为每个
<div>使用完全相同的“id”值,一方面。元素的“id”值在页面中应该始终是完全唯一的。你可以保留一个计数器并给出由“center_”加上数字组成的“id”值,或者类似的东西。 -
你建议怎么做?
-
不要让“id”值成为常量,而是从循环中的 PHP 变量动态创建它。