简体   繁体   English

如何重定向到其他页面并在点击时动态加载相应ID的内容

[英]how to redirect to other page and dynamically load content of corresponding id on click

i'm working on a project and i want to do following: on the home page i have divs which are dynamically loaded from database, now when i click on the div i want it to redirect to new page and then load corresponding content only, from database, just like a blog website, when you click on an article on homepage and it loads an article from database on the new page, i know it must be a simple process but i couldn't find how to write all that code, so can anyone help me with that? 我正在做一个项目,我想执行以下操作:在主页上,我有从数据库动态加载的div,现在当我单击div时,我希望它重定向到新页面,然后仅加载相应的内容,从数据库开始,就像在博客网站上一样,当您单击主页上的文章并在新页面上从数据库中加载文章时,我知道这一定是一个简单的过程,但是我找不到如何编写所有代码的方法,有人可以帮我吗?

thank you in advance 先感谢您

this is my home page: 这是我的主页:

<?php
require_once 'core/init.php';
include 'includes/head.php';
include 'includes/nav.php';
include 'includes/section1.php';

$sql = "SELECT * FROM topics WHERE featured = 1";
$featured = $db->query($sql);?>

  <section class="s2">
     <div class="grid_container">

        <?php while($article = mysqli_fetch_assoc($featured)) : ?>
        <div class="floating-box" onclick="loadart(<?= $article ['id']; ?>)">
           <img src="<?= $article ['image']; ?>" class="img-responsive" alt="<?= $article ['title']; ?>">
           <div class="grid_content_wrapper">
              <br>
              <p><?= $article ['subtitle']; ?></p>
              <h3><?= $article ['title']; ?></h3>
              <p><?= $article ['description']; ?></p>
           </div>
        </div>
        <?php endwhile; ?>

     </div>
  </section>


  <section class="s3">
     <a  href="single.php"><button  class="more_posts"><h3>MORE AMAZING THINGS</h3></button></a>
  </section>

<?php
include 'includes/footer.php';?>

now when i click on the div floating box i want it to redirect to this page and load content from database depending on the div i clicked i used this script but it only redirects and loads all content from database but i want it to load based on id of the corresponding div: 现在,当我单击div浮动框时,我希望它重定向到此页面并根据我单击的div加载数据库中的内容,我使用了此脚本,但它仅重定向并加载了数据库中的所有内容,但我希望它根据加载相应的div的ID:

function loadart(id) { var data = {"id" : id}; 函数loadart(id){var data = {“ id”:id}; window.location.href = "single.php";} window.location.href =“ single.php”;}

So let's say you have <div class="clickDiv">www.somedomain.com</div> 假设您有<div class="clickDiv">www.somedomain.com</div>

You want to set up an event handler that will listen for it, find out what domain is in the div and then redirect the user to that URL. 您想要设置一个事件处理程序来监听它,找出div中的域,然后将用户重定向到该URL。

'$(".clickDiv").mousedown(function(){
var url = $(this).text();
window.location.href = url;
});

So what has happened here is that if all of your divs have the class "ClickDiv", the event listener knows when it's been clicked on and then redirects to the URL in that div. 因此,这里发生的是,如果您所有的div都具有“ ClickDiv”类,则事件侦听器会知道何时单击该事件,然后将其重定向到该div中的URL。

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

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