简体   繁体   English

如何使用JavaScript / JQuery在页面上显示html框?

[英]How to make a html box appear on page using JavaScript/JQuery?

How can I open an section of html with an id using JS/JQuery? 如何使用JS / JQuery打开具有ID的html部分?

Lets say the html I want to trigger with JS/JQuery is 可以说我想用JS / JQuery触发的html是

<section class="modal--show" id="show"
        tabindex="-1" role="dialog" aria-labelledby="label-show" aria-hidden="true">

    <div class="modal-inner">
        <header>
            <h2 id="label-show">A modal</h2>
        </header>

        <div class="modal-content">
            <p>Content.</p>
               <div id="badgeselect">

        </div>

        <footer>
            <p>Footer</p>
        </footer>
    </div>

</section>

I've tried using 我试过使用

 $('#show').dialog('open');

which doesn't work. 这不起作用。 The only other consideration is, that the full code must also copy an image that the user clicks on (to activate show ) 唯一的其他考虑是,完整代码还必须复制用户单击的图像(以激活show

I have this at the moment, the only part not working is making the above code show on the page after the user has clicked the image. 目前,我已经有了这个功能,唯一无法正常工作的部分是在用户单击图像后在页面上显示了上面的代码。

    $(document).ready(function() {
        $('.go img').css('cursor', 'pointer');
        $('.go').on('click', 'img', function(e) {
            $(this).width(100).height(100).appendTo('#badgeselect');

              $('#show').dialog('open');

            });
            SaveMyBadge();
            return false;
        });

You are missing a closing div tag. 您缺少结束div标签。

<div id="badgeselect">

is never closed. 永远不会关闭。 That could be causing some strange issues 那可能会引起一些奇怪的问题

Well you could just add a click listener to the img and change the css class of the section. 好吧,您可以将点击侦听器添加到img并更改该部分的css类。 The css class could have display:block CSS类可能具有display:block

and as you stated its a pop up you could set its position in your class along with other properties such as opacity or simply 当您说出它的弹出窗口时,可以设置其在类中的位置以及其他属性,例如不透明度或简单地

document.getElementById("show").style.display = block;

OR 要么

document.getElementById("show").className = "MyClass";

.MyClass{
   display:block;
   position:absolute;
   top:50%;
   left:50%;
   margin-left:-width/2;
   margin-top:-height/2;
   opacity:0.8;
}

This should create a pop up like feature. 这应该会创建一个弹出式功能。

.dialog is not a native method available in JavaScript. .dialog不是JavaScript中可用的本机方法。 If you want a modal out of the box then i recommend using a JavaScript Library which comes with such features built in. Twitter's bootstrap will be a good option. 如果您想开箱即用的modal ,那么我建议使用内置有此类功能的JavaScript库。Twitter的引导程序将是一个不错的选择。

To get started: 开始:
In your <head> 在你的<head>

<link rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css">

Include this script after you include jQuery 包含jQuery之后,包含此脚本

<script src="//maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>  

HTML: HTML:

<!-- Modal Markup -->
<div class="modal fade" id="modal">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
        <h4 class="modal-title">Modal title</h4>
      </div>
      <div class="modal-body">
        <p>One fine body&hellip;</p>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
        <button type="button" class="btn btn-primary">Save changes</button>
      </div>
    </div><!-- /.modal-content -->
  </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

To launch the modal 启动模态

$('#modal').modal({'show':true});

Bootstrap homepage 引导首页

UPDATE: 更新:

$('.go').on('click', 'img', function(e) {
    $(this).width(100).height(100).appendTo('#badgeselect');
    $('#modal').modal({'show':true});
});

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

相关问题 如何使箭头错误消息出现以使用jQuery / Java进行表单验证 - How to make an arrow box error message appear for form validation using jQuery/Javascript 如何使由javascript创建的html元素显示在页面顶部? - How to make html elements created by javascript to appear at the top of page? 使用JQuery无法显示模式框 - Unable to make modal box appear using JQuery 如何使HTML表单显示在另一个HTML页面顶部的框中 - How do I make an html form appear in a box on top of another html page 如何使用javascript在html中只出现一次弹出窗口 - how to make a pop up appear only once in html using javascript 如何使用JavaScript在HTML中仅显示背景图像一次 - How to make background image appear only once in HTML using JavaScript 如何使用Javascript使某些内容缓慢显示在页面上? - How do you make something to appear slowly on a page using Javascript? 如何仅使用javascript制作固定的div框(无CSS,jQuery,HTML) - How to make a fixed div box using javascript ONLY (no css, jquery, html) 如何使用 javascript/jquery 使文本显示为所需的 innerHTML - How to make a text appear with a desired innerHTML using javascript/jquery 如何使用 Jquery 或 javascript 使 A 中的表单输入值出现在 B 中 - How to make form input value in A to appear in B using Jquery or javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM