简体   繁体   English

jQuery:.dialog()不适用于.classe选择器的下一个元素

[英]JQuery : .dialog() not working for next element of .classe selector

I use a php while loop to generate some items with a litle description. 我使用php while循环来生成一些带有点描述的项目。 Each item has the same class and are preceded by an image of it. 每个项目都具有相同的类别,并在其前面带有图像。

The objective is to open a dialog that contains the description of the item when I click on the corresponding image. 目的是打开一个对话框,其中包含当我单击相应图像时该项目的描述。

This is what I've done: 这是我所做的:

Generate all items with all pictures: 生成所有图片的所有项目:

        while($result = $req->fetch()) {?>
                <img src="./css/images/icon/<?=$result['category']?>/<?=$result['rarity']?>" alt="<?=$result['object']?>" title="<?=$result['object']?>" class="obj">
                <p class="descr_obj">
                    Name : <?=$result['objet']?><br><br>
                    Rarity : <?=str_replace(".png", "", $result['rarity'])?><br><br>
                    Description :<br>
                    <?=$result['description']?>
                </p>

        <?php }?>

Generate JQuery dialog: 生成JQuery对话框:

$(".descr_obj").dialog({
        autoOpen: false
    })

    $(".obj").click(function(){
        text = $(this).next(".descr_obj").text();
        $(text).dialog("open");
    });

But nothing is appearing and the console doesn't show any error... 但是什么也没出现,控制台也没有显示任何错误...

Can you please, help me? 你能帮我么? Thank you in advance. 先感谢您。

Couple of issues. 几个问题。 The main one is that you never actually tell the dialog to open (by using .dialog("open") . The second one is you're going to end up with a ton of dialogs - one for each img because you're repeating the paragraph. Each instance of the descr_obj class will get it's own. 主要的一点是,您实际上从不会告诉对话框要打开(通过使用.dialog("open") ,第二个是您最终将获得大量对话框-每个img一次,因为您要重复descr_obj类的每个实例都会拥有它自己的段落。

Try something like this (my php sucks, so it's probably wrong): 尝试这样的事情(我的php很烂,所以可能是错误的):

while($result = $req->fetch()) {?>
    <img src="./css/images/icon/<?=$result['category']?>/<?=$result['rarity']?>" 
         alt="<?=$result['object']?>" 
       title="<?=$result['object']?>" 
       class="obj" 
   data-name="<?=$result['objet']?>"
 data-rarity="<?=str_replace(".png", "", $result['rarity'])?>" 
   data-desc="<?=$result['description']?>">
<?php }?>
<p class="descr_obj">
    Name : <span class="name"></span><br><br>
    Rarity : <span class="rarity"></span><br><br>
    Description :<br>
    <span class="desc"></span>
</p>

But here is what it the JavaScript would end up looking like: 但是,JavaScript最终看起来像这样:

 $(function() { let dialog = $(".descr_obj").dialog({ autoOpen: false }); $(".obj").click(function() { for(let prop of Object.keys(this.dataset)) { dialog.find(`span.${prop}`).html(this.dataset[prop]); } dialog.dialog("open") }); }); 
 <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script> <img src="https://i.stack.imgur.com/4fDk5.png" data-name="one" data-rarity="Super Rare" data-desc="Amazing" class="obj"> <img src="https://i.stack.imgur.com/xBUJX.png" data-name="two" data-rarity="Common" data-desc="Meh" class="obj"> <p class="descr_obj"> Name : <span class="name"></span><br><br> Rarity : <span class="rarity"></span><br><br> Description :<br> <span class="desc"></span> </p> 

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

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