search
HomeWeb Front-endHTML TutorialBootStrap学习(6)_模态框_html/css_WEB-ITnose

一、模态框

模态框(Modal)是覆盖在父窗体上的子窗体。通常,目的是显示来自一个单独的源的内容,可以在不离开父窗体的情况下有一些互动。子窗体可提供信息、交互等。

如果只使用该功能,只引入BootStrap中的 modal.js即可

1.用法:

您可以切换模态框(Modal)插件的隐藏内容:

  • 通过 data 属性:在控制器元素(比如按钮或者链接)上设置属性 data-toggle="modal",同时设置 data-target="#identifier" 或href="#identifier" 来指定要切换的特定的模态框(带有 id="identifier")。
  • 通过 JavaScript:使用这种技术,您可以通过简单的一行 JavaScript 来调用带有 id="identifier" 的模态框:
    $('#identifier').modal(options)
  • <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>   <meta name="viewport" content="width=device-width, initial-scale=1.0"/>    <title></title>    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />    <script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script></head><body style="margin-top:20px;margin-left:20px;">       <h3 id="创建模态框-Modal">创建模态框(Modal)</h3><!-- 按钮触发模态框 -->   <table class="table table-bordered">   <thead>      <tr>         <th>书名</th>         <th>单价</th>        <th>操作</th>      </tr>   </thead>   <tbody>      <tr>         <td>三国演义</td>         <td>50</td>          <td ><button class="btn btn-primary btn-lg" data-toggle="modal"    data-target="#myModal">   修改</button></td>      </tr>   </tbody></table><!-- 模态框(Modal) -->    <form ><div class="modal fade" id="myModal" tabindex="-1" role="dialog"    aria-labelledby="myModalLabel" aria-hidden="true">   <div class="modal-dialog">      <div class="modal-content">         <div class="modal-header">            <button type="button" class="close"                data-dismiss="modal" aria-hidden="true">                  &times;            </button>            <h4 id="模态框-Modal-标题">               模态框(Modal)标题            </h4>         </div>         <div class="modal-body">            <table class="table table-condensed">            <tr><td>书名:<input type="text" value="三国演义"/>价格:<input type="text" value="50" /></td></tr>                </table>          </div>         <div class="modal-footer">            <button type="button" class="btn btn-default"                data-dismiss="modal">关闭            </button>            <button  type="button" class="btn btn-primary">               提交更改            </button>         </div>      </div>       <!-- /.modal-content --></div><!-- /.modal -->    </div> </form></body></html>

    效果:

     代码讲解:

  • 使用模态窗口,您需要有某种触发器。您可以使用按钮或链接。这里我们使用的是按钮。
  • 如果您仔细查看上面的代码,您会发现在
  • 在模态框中需要注意两点:
  • 第一是 .modal,用来把
    的内容识别为模态框。
  • 第二是 .fade class。当模态框被切换时,它会引起内容淡入淡出。
  • aria-labelledby="myModalLabel",该属性引用模态框的标题。
  • 属性 aria-hidden="true" 用于保持模态窗口不可见,直到触发器被触发为止(比如点击在相关的按钮上)。
  • class="close",close 是一个 CSS class,用于为模态窗口的关闭按钮设置样式。
  • data-dismiss="modal",是一个自定义的 HTML5 data 属性。在这里它被用于关闭模态窗口。
  • class="modal-body",是 Bootstrap CSS 的一个 CSS class,用于为模态窗口的主体设置样式。
  • class="modal-footer",是 Bootstrap CSS 的一个 CSS class,用于为模态窗口的底部设置样式。
  • data-toggle="modal",HTML5 自定义的 data 属性 data-toggle 用于打开模态窗口。
  • 选项: 在点出模态窗的按钮上加上:data-backdrop='static'  可以指定一个静态的背景,当用户点击模态框外部时不会关闭模态框。

    2.事件

    下表列出了模态框中要用到事件。这些事件可在函数中当钩子使用。

    事件 描述 实例
    show.bs.modal 在调用 show 方法后触发。
    $('#identifier').on('show.bs.modal', function () {  // 执行一些动作...})
    shown.bs.modal 当模态框对用户可见时触发(将等待 CSS 过渡效果完成)。
    $('#identifier').on('shown.bs.modal', function () { // 执行一些动作...})
    hide.bs.modal 当调用 hide 实例方法时触发。
    $('#identifier').on('hide.bs.modal', function () { // 执行一些动作...})
    hidden.bs.modal 当模态框完全对用户隐藏时触发。
    $('#identifier').on('hidden.bs.modal', function () { // 执行一些动作...})

     

     

     

     

     

     

     

     

     

     

     

     

     show方法:即弹出模态框事件

    hide:方法:即关闭模态框事件

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta name="viewport" content="width=device-width, initial-scale=1.0" />    <title></title>    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />    <script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script></head><body style="margin-top: 20px; margin-left: 20px;">    <h3 id="创建模态框-Modal">创建模态框(Modal)</h3>    <!-- 按钮触发模态框 -->    <table class="table table-bordered">        <thead>            <tr>                <th>书名</th>                <th>单价</th>                <th>操作</th>            </tr>        </thead>        <tbody>            <tr>                <td>三国演义</td>                <td>50</td>                <td>                    <button class="btn btn-primary btn-lg" data-toggle="modal"                        data-target="#myModal" data-backdrop="static">                        修改                    </button>                </td>            </tr>        </tbody>    </table>    <!-- 模态框(Modal) -->    <div class="modal fade" id="myModal" tabindex="-1" role="dialog"        aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="true">        <div class="modal-dialog">            <div class="modal-content">                <div class="modal-header">                    <button type="button" class="close"                        data-dismiss="modal" aria-hidden="true">                        &times;                    </button>                    <h4 id="模态框-Modal-标题">模态框(Modal)标题                    </h4>                </div>                <div class="modal-body">                    <table class="table table-condensed">                        <tr>                            <td>书名:<input type="text" value="三国演义" />价格:<input type="text" value="50" /></td>                        </tr>                    </table>                </div>                <div class="modal-footer">                    <button type="button" class="btn btn-default"                        data-dismiss="modal">                        关闭                    </button>                    <button type="button" class="btn btn-primary">                        提交更改                    </button>                </div>            </div>            <!-- /.modal-content -->        </div>        <!-- /.modal -->    </div>   <script>       $(function () {           $('#myModal').on('hide.bs.modal', function () {               alert('真的要取消修改吗...');           })       });</script></body></html>

     

     

    效果:

     

    点关闭时会触发事件。

    三、提示工具

    是基于BootStrap里面的(Tooltip)插件, 如果单单想用这个功能,可以直接用 tooltip.js这个插件.

    当您想要描述一个链接的时候,提示工具(Tooltip)就显得非常有用。

    1.用法有两种

       1.1 通过 data 属性:如需添加一个提示工具(tooltip),只需向一个锚标签添加 data-toggle="tooltip" 即可。锚的 title 即为提示工具(tooltip)的文本。默认情况下,插件把提示工具(tooltip)设置在顶部。

    <a href="#" data-toggle="tooltip" title="提示信息">把鼠标停在我的上面</a>

      1.2 通过 JavaScript:通过 JavaScript 触发提示工具(tooltip):

    $('#identifier').tooltip(options)<br /><strong>注意:</strong> 您必须使用 jquery 激活它(读取 javascript)。使用下面的脚本来启用页面中的所有的提示工具(tooltip):<br />
    $(function () { $("[data-toggle='tooltip']").tooltip(); });

                                左侧的 Tooltip.    顶部的 Tooltip.    底部的 Tooltip.    右侧的 Tooltip

    提示工具(Tooltip)插件 - 按钮

    <script> $(function () { $(&quot;[data-toggle='tooltip']&quot;).tooltip(); });</script>

    效果:

     

    2.提示工具的方法 

    方法 描述 实例
    Options: .tooltip(options) 向元素集合附加提示工具句柄。
    $().tooltip(options)
    Toggle: .tooltip('toggle') 切换显示/隐藏元素的提示工具。
    $('#element').tooltip('toggle')
    Show: .tooltip('show') 显示元素的提示工具。
    $('#element').tooltip('show')
    Hide: .tooltip('hide') 隐藏元素的提示工具。
    $('#element').tooltip('hide')
    Destroy: .tooltip('destroy') 隐藏并销毁元素的提示工具。
    $('#element').tooltip('destroy')

     

     

     

     

     

     

     

     

     

    <!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />    <title></title>    <link href="http://apps.bdimg.com/libs/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" />    <script src="http://apps.bdimg.com/libs/jquery/2.0.0/jquery.min.js"></script>    <script src="http://apps.bdimg.com/libs/bootstrap/3.3.0/js/bootstrap.min.js"></script></head>    <body ><div style="padding: 100px 100px 10px;">    <a href="#" class="tooltip-show" data-toggle="tooltip"       title="show">Tooltip 方法 show   </a><br /><br />   <a href="#" class="tooltip-hide" data-toggle="tooltip"       data-placement="left" title="hide">Tooltip 方法 hide   </a><br /><br /> <a href="#" class="tooltip-destroy" data-toggle="tooltip"       data-placement="top" title="destroy">Tooltip 方法 destroy   </a><br /><br />   <a href="#" class="tooltip-toggle" data-toggle="tooltip"       data-placement="bottom" title="toggle">Tooltip 方法 toggle   </a><br /><br />   <p class="tooltip-options" >     <a href="#" data-toggle="tooltip" title="'am Header2">Tooltip 方法 options      </a>   </p>   <script>       $(function () { $('.tooltip-show').tooltip('show'); });       $(function () { $('.tooltip-hide').tooltip('hide'); });       $(function () { $('.tooltip-destroy').tooltip('destroy'); });       $(function () { $('.tooltip-toggle').tooltip('toggle'); });       $(function () {           $(".tooltip-options a").tooltip({ html: true });       });   </script></div></body></html>

    效果:

     

     

     

    Statement
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    From Text to Websites: The Power of HTMLFrom Text to Websites: The Power of HTMLApr 13, 2025 am 12:07 AM

    HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.

    Understanding HTML, CSS, and JavaScript: A Beginner's GuideUnderstanding HTML, CSS, and JavaScript: A Beginner's GuideApr 12, 2025 am 12:02 AM

    WebdevelopmentreliesonHTML,CSS,andJavaScript:1)HTMLstructurescontent,2)CSSstylesit,and3)JavaScriptaddsinteractivity,formingthebasisofmodernwebexperiences.

    The Role of HTML: Structuring Web ContentThe Role of HTML: Structuring Web ContentApr 11, 2025 am 12:12 AM

    The role of HTML is to define the structure and content of a web page through tags and attributes. 1. HTML organizes content through tags such as , making it easy to read and understand. 2. Use semantic tags such as, etc. to enhance accessibility and SEO. 3. Optimizing HTML code can improve web page loading speed and user experience.

    HTML and Code: A Closer Look at the TerminologyHTML and Code: A Closer Look at the TerminologyApr 10, 2025 am 09:28 AM

    HTMLisaspecifictypeofcodefocusedonstructuringwebcontent,while"code"broadlyincludeslanguageslikeJavaScriptandPythonforfunctionality.1)HTMLdefineswebpagestructureusingtags.2)"Code"encompassesawiderrangeoflanguagesforlogicandinteract

    HTML, CSS, and JavaScript: Essential Tools for Web DevelopersHTML, CSS, and JavaScript: Essential Tools for Web DevelopersApr 09, 2025 am 12:12 AM

    HTML, CSS and JavaScript are the three pillars of web development. 1. HTML defines the web page structure and uses tags such as, etc. 2. CSS controls the web page style, using selectors and attributes such as color, font-size, etc. 3. JavaScript realizes dynamic effects and interaction, through event monitoring and DOM operations.

    The Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesThe Roles of HTML, CSS, and JavaScript: Core ResponsibilitiesApr 08, 2025 pm 07:05 PM

    HTML defines the web structure, CSS is responsible for style and layout, and JavaScript gives dynamic interaction. The three perform their duties in web development and jointly build a colorful website.

    Is HTML easy to learn for beginners?Is HTML easy to learn for beginners?Apr 07, 2025 am 12:11 AM

    HTML is suitable for beginners because it is simple and easy to learn and can quickly see results. 1) The learning curve of HTML is smooth and easy to get started. 2) Just master the basic tags to start creating web pages. 3) High flexibility and can be used in combination with CSS and JavaScript. 4) Rich learning resources and modern tools support the learning process.

    What is an example of a starting tag in HTML?What is an example of a starting tag in HTML?Apr 06, 2025 am 12:04 AM

    AnexampleofastartingtaginHTMLis,whichbeginsaparagraph.StartingtagsareessentialinHTMLastheyinitiateelements,definetheirtypes,andarecrucialforstructuringwebpagesandconstructingtheDOM.

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    AI Hentai Generator

    AI Hentai Generator

    Generate AI Hentai for free.

    Hot Article

    R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. Best Graphic Settings
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    R.E.P.O. How to Fix Audio if You Can't Hear Anyone
    3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
    WWE 2K25: How To Unlock Everything In MyRise
    4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

    Hot Tools

    SublimeText3 Chinese version

    SublimeText3 Chinese version

    Chinese version, very easy to use

    mPDF

    mPDF

    mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

    Dreamweaver Mac version

    Dreamweaver Mac version

    Visual web development tools

    SecLists

    SecLists

    SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.