简体   繁体   English

如何用java脚本显示一个div

[英]How to show a div with java script

I have a simple problem.我有一个简单的问题。 I need to use php which will generate products on a page.我需要使用 php 来在页面上生成产品。 When the user clicks on quick look, it should popup a window and show some product information.当用户点击快速查看时,它应该弹出一个窗口并显示一些产品信息。 I can only do this two ways: first, generate a popup div for each product.我只能通过两种方式做到这一点:首先,为每个产品生成一个弹出式 div。 Second, generate only one basic pop-up and fill in data from the product.其次,只生成一个基本的弹出窗口并填写来自产品的数据。 Now I have the second version:现在我有了第二个版本:

This is the div:这是div:

<div class="fixedModalQuickLook">
        <a href="javascript:void(0)" class="close-quick-look">close <i class="fa fa-times"></i></a>
        <div class="innerModalQuickLook">
            <div class="shortInfoItemInfo visible-xs col-xs-12">
                <h2><a href="#" title="">  do</a></h2>
            </div>
            <div class="containerImg-Social col-lg-4 col-md-4 col-sm-6 col-xs-12">
                <div class="imgWishListItem ">
                    <a href="#" title="">
                        <img src="images/gallery/carti2.jpg" class="img-responsive" />
                    </a>
                </div>
                <div class="shareItem ">
                    <ul>
                        <li><a href="#" title=""><i class="fa fa-facebook"></i></a> </li>
                        <li><a href="#" title=""><i class="fa fa-google-plus"></i></a> </li>
                        <li><a href="#" title=""><i class="fa fa-twitter"></i></a> </li>
                    </ul>
                </div>
            </div>

            <div class="shortInfoItemInfo col-lg-3 col-md-3 col-sm-6 col-xs-12">
                <div class="buttonCartWishList visible-xs"> <a href="javascript:void(0)" class="addToCartButtonWL"><input type="text" placeholder="1" name="count" /> Add to cart</a></div>
                <h2 class="hidden-xs"><a href="#" title="">  Hiroshige: One Hundred Famous Views of Edo</a></h2>

                <div class="stars"><img src="images/icon/stars-small.png" alt=" " title="" /> </div>
                <div class="priceQuikLookItem">&#163;30.88</div>
                <div class="buttonCartWishList hidden-xs">
                    <a href="javascript:void(0)" class="addToCartButtonWL">

                        <form class="wrapInput">   <input type="text" placeholder="1" size="1" id="count" onkeypress="this.style.width = ((this.value.length + 12) * 6) + 'px';" /> </form><!--</div>--> Add to cart
                    </a>
                </div>   <!--onkeypress="this.style.width = ((this.value.length + 12) * 9) + 'px';" -->
            </div>

            <div class="stocInfo col-lg-2 col-md-2 col-sm-6 col-xs-12">
                <h5>disponibility</h5>
                <div class="stocNumber"> Stoc:<span>100</span> </div>
                <div class="multipleOptions">
                    <div class="showInfo">
                        <div class="color cyan"></div>
                        <div class="color azure"></div>
                        <div class="color skyBlue"></div> <i class="fa fa-plus"></i>
                    </div>
                    <div class="showPlusInfo">
                        <h6>Colors</h6>
                        <div class="color cyan"></div>
                        <div class="color azure"></div>
                        <div class="color skyBlue"></div>
                        <div class="color phthaloBlue"></div>
                        <div class="color sapphire"></div>
                        <div class="color gold"></div>
                        <div class="color chartreuse"></div>
                        <div class="color jasmine"></div>
                        <div class="color apricot"></div>
                        <div class="color gold"></div>
                        <div class="color chartreuse"></div>
                        <div class="color azure"></div>
                        <div class="color skyBlue"></div>
                        <div class="color phthaloBlue"></div>
                        <div class="color jasmine"></div>
                        <div class="color apricot"></div>
                        <div class="color phthaloBlue"></div>
                        <div class="color sapphire"></div>
                        <div class="color gold"></div>
                        <div class="color chartreuse"></div>
                        <div class="color jasmine"></div>
                        <div class="color apricot"></div>
                        <div class="color gold"></div>
                        <div class="color chartreuse"></div>
                        <div class="color jasmine"></div>
                        <div class="color apricot"></div>
                    </div>
                </div>
                <div class="multipleOptions">
                    <div class="showInfo">shipping<i class="fa fa-plus"></i>  </div>
                    <div class="showPlusInfo">
                        <h6>Shipping</h6>
                        <p>Sed ut perspiciatis unde omnis</p>
                        <p>Sed ut perspiciatis unde omnis iste natus error sit voluptatem</p>
                    </div>
                </div>
            </div>

            <div class="moreInfo  col-lg-3 col-md-3 col-sm-6 col-xs-12">
                <h5>Short Info</h5>
                <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. </p>
                <div class="listInfoItem">
                    <ul>
                        <li><strong>Gama:</strong> PowerSeeker<br></li>
                        <li><strong>Design optic:</strong> Refractor<br></li>
                        <li><strong>Nivel:</strong> Incepatori/Copii<br></li>
                        <li><strong>Tip obiectiv:</strong> Acromat<br></li>
                        <li><strong>Computerizat:</strong> Nu<br></li>
                    </ul>
                    <ul>
                        <li><strong>Magnitudine stelara maxima:</strong> 11.1<br></li>
                        <li><strong>Marire teoretica maxima:</strong> 120x<br></li>
                        <li><strong>Diametru (inch):</strong> 50 mm<br></li>
                        <li><strong>Montura:</strong> Altazimutala<br></li>
                        <li><strong>Distanta focala:</strong> 600 mm<br></li>
                    </ul>
                </div>
            </div>

     </div>
 </div>

And with this I show div:有了这个,我显示了div:

$(".close-quick-look").on("click", function () {
  $(".quickModalCover").css('display', 'none');
  $(".fixedModalQuickLook").css('display', 'none');
});
$(".buttonQuickLook").on("click", function () {
  $(".quickModalCover").css('display', 'block');
  $(".fixedModalQuickLook").css('display', 'block');
  $(".fixedModalQuickLook").css('opacity', '1');
});

I don't know how to show the specific div, for example I will generate:我不知道如何显示特定的div,例如我会生成:

<div class="fixedModalQuickLook17">
    <a href="javascript:void(0)" class="close-quick-look">close <i class="fa fa-times"></i></a>
    <div class="innerModalQuickLook17"></div>
</div>

<div class="fixedModalQuickLook30">
    <a href="javascript:void(0)" class="close-quick-look">close <i class="fa fa-times"></i></a>
   <div class="innerModalQuickLook30"></div>
</div>

<div class="fixedModalQuickLook556">
    <a href="javascript:void(0)" class="close-quick-look">close <i class="fa fa-times"></i></a>
    <div class="innerModalQuickLook556"></div>
</div>

And how i can modifiy my javascript to detect what i click to show, or how can i make a single div and fill that with my php info.以及我如何修改我的 javascript 以检测我点击显示的内容,或者我如何制作一个 div 并用我的 php 信息填充它。 Thanks !谢谢 !

Looking at your live site.查看您的实时站点。 I think this would help you.我想这会对你有所帮助。

What are we doing here:我们在这里做什么:

  • First reaching the parent element of button .首先到达button的父元素。 Here li is parent of .buttonQuickLook .这里li.buttonQuickLook.buttonQuickLook
  • Now looking for the next element which has class fixedModalQuickLook .现在寻找下一个具有fixedModalQuickLook类的fixedModalQuickLook
  • Finally .addClass('display') which has display:block as property.最后.addClass('display')具有display:block作为属性。 (Add to your CSS) (添加到您的 CSS)

Okay almost done here, now this would work.好的,这里差不多完成了,现在这将起作用。 But still one safe sided code line needed.但仍然需要一个安全的代码行。

  • Before doing the above thing we will close all Quick Look Modals (which is not necessary, as you need to close prev to open next).在执行上述操作之前,我们将关闭所有 Quick Look Modals (这不是必需的,因为您需要关闭 prev 才能打开 next)。 But still we will closing them(just in case somehow) by .addClass('display')但是我们仍然会通过.addClass('display')关闭它们(以防万一.addClass('display')

Demo : Here i have used a tag as click Listener.演示:这里我使用a标签作为点击监听器。 Just an example.只是一个例子。

Here is the solution:这是解决方案:

$('.buttonQuickLook').click(function(e){
   $('.fixedModalQuickLook').removeClass("display");  // Removing all previous one if somehow still open.
   $(this).closest('li').next('.fixedModalQuickLook').toggleClass("display");
});

Add this to your CSS:将此添加到您的 CSS:

.display
{
    display:block;
}

It's actually quite simply in jQuery you can easily use the toggle() function to literally change the visibility of any targeted element:它实际上在 jQuery 中非常简单,您可以轻松地使用toggle()函数从字面上更改任何目标元素的可见性:

This makes show/hide really easy as the following show.这使得显示/隐藏非常容易,如下所示。

For example:例如:

HTML HTML

<div class="fixedModalQuickLook">
    <a href="javascript:void(0)" class="close-quick-look">close <i class="fa fa-times"></i></a>
    <div class="innerModalQuickLook">
      <div class="shortInfoItemInfo visible-xs col-xs-12">
        <h2><a href="#" title="">  do</a></h2>
      </div>
    </div>
</div>


  <button>Quick Look</button>

JS JS

$(".close-quick-look, button").on("click", function() {

  $(".quickModalCover").toggle();
  $(".fixedModalQuickLook").toggle()

})

Here is the full working example: http://jsbin.com/babupucoku/edit?html,js,output这是完整的工作示例: http : //jsbin.com/babupucoku/edit?html,js,output

Hope that helps!希望有帮助!

some psuedo code:一些伪代码:

<? foreach($products as $product):?>
   <div id ="product_<?=$product->id;?>" onclick="display('product_<?=$product_id;?>'); return false;">
      <div id="header">
         Viewing information for <?=$product->name;?>
      </div>
   </div>
<? endforeach;?>

jquery:查询:

 function hide()
 {
     $('[id^=product_]').hide();
 }

$(document).ready(function() {
        hide();
    });

//displays div passed in 
function display(div_id)
{
   hide();
   $('#'+div_id).fadeIn();
}

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

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