繁体   English   中英

如何将CMS PHP代码与jQuery fancybox结合在一起?

[英]How to combine the CMS PHP code with jQuery fancybox?

我是编程新手,尝试制作简单的cms:

index.php显示所有文章主题,
用户单击主题以在Fancybox中显示内容,而不是转发到新页面。

我使PHP代码能够正常工作(但它已转发到新页面),并测试https://stackoverflow.com/a/7844043/1775888 (这可以在地址栏中创建Fancybox内容自己的URL)也可以工作。

我不知道如何合并它们?


我之前尝试过:在PHP中更改href,单击未设置主题$ _GET [id],然后无法获取查询。

任何建议,我都非常感谢您抽出宝贵的时间。


PHP(index.php)

if(isset($_GET[id])){
    $id=mysql_real_escape_string($_GET["id"]);
    $sql="select * from $table where id='$id'";
    $query=mysql_query($sql) or die(mysql_error());
    while($list=mysql_fetch_array($query)){
            print"
<div class=\"contentwrap\" align=\"center\">
    <div class=\"content\">\"$list[content]\"</div>
</div>
    ";
        }
}
else{
    $sql="select * from $table order by id desc";
    $query=mysql_query($sql) or die(mysql_error());
    while($list=mysql_fetch_array($query)){
            print"
<div class=\"subjectwrap\" align=\"center\">
    <div class=\"subject\"><a href=\"index.php?id=$list[id]\">$list[subject]</a></div>
</div>
    ";
    }
}

jQuery(来自https://stackoverflow.com/a/7844043/1775888

function showfancybox(id) {
    switch(id) {
        case '#_name':

        $(id).show();

        $.fancybox({
            href: id,
            type:'inline',


            onClosed: function() {
                $(id).hide();
            }
        });
    break;
    }
}

showfancybox(location.hash);

$('a.flink').click(function(e){
    showfancybox($(this).attr('href')); 
});


编辑:
我更改代码,让$ list [content]加载并显示:none。
进入index.php,获取消息:无法加载请求的内容。 请稍后再试。

<?php
$sql="select * from $table order by id desc";
    $query=mysql_query($sql) or die(mysql_error());
    while($list=mysql_fetch_array($query)){
        print"
    <div class=\"subjectwrap\">
        <div class=\"subject\"><a class=\"flink\" href=\"#$list[id]\">$list[subject]</a></div>
    </div>
    ";
    print"
<div class=\"atc\" id=\"$list[id]\">
    <div class=\"contentwrap\" align=\"center\">
        <div class=\"content\">\"$list[content]\"</div>
    </div>
</div>
    ";
    }
?> 

<script type="text/javascript">
$(function(){
function showfancybox(id) {
    switch(id) {
        case '<?php "#$list[id]" ?>':

        $(id).show();

        $.fancybox({
            href: id,
            type:'inline',

            onClosed: function() {
                $(id).hide();
            }
        });
    break;
    }
}

showfancybox(location.hash);

$('a.flink').click(function(e){
    showfancybox($(this).attr('href')); //make href to id
});         
});
</script>

尝试调用e.preventDefault(); 在单击事件中调用其他函数之前,标签arent分配了在选择器中使用的flink类

还可以在get ['id']上调用intval()而不是escape_string,

对不起,但我不愿回答您的问题。 您的代码似乎全错了。

例如,您甚至不知道为什么有这条线? --

case '<?php "#$list[id]" ?>':

您认为这是为了什么? 您认为它如何执行? 那是JavaScript片段在php循环中吗? 如果是,那么您将获得具有相同名称的多个功能。

除此之外,如果您的目标是基于url哈希值打开/关闭fancyBox,那么我建议使用(即将公开发布)历史记录帮助程序,请参见以下示例-http: //jsfiddle.net/FB7UW/show/light/ (http://jsfiddle.net/FB7UW/)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM