带有 html 内容的 bootstrap popover [英] bootstrap popover with html content

查看:76
本文介绍了带有 html 内容的 bootstrap popover的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 bootstrap popover 内容与 html 属性分开,就像您可以使用其他组件一样,但我无法让它工作......

<html lang="zh-cn"><头><title>引导程序示例</title><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1"><link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script><script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script><身体><div class="容器"><h3>弹出示例</h3><p>Popovers 不是纯 CSS 插件,因此必须使用 jQuery 进行初始化:选择指定的元素并调用 popover() 方法.</p><div class="popover" ><a href="#" class="popover-toggle" title="Popover Header" >Toggle popover</a><div class="popover-content">我在这里

<脚本>$(document).ready(function(){$('[data-toggle="popover"]').popover();});</html>

解决方案

您有简单的锚点和一些不应显示的 div.

<a href="#" id="tglr" class="popover-toggle" title="Popover Header">Toggle popover</a><div id="customdiv" style="display: none">这里<b>I</b>是

在 JS 中,我们获取锚点(通过 id)并使用 2 个选项在其上安装弹出框:第一个 'html' - 允许显示 html 而不是简单的文本,第二个指定从我们的 div 中获取的内容:

$(document).ready(function(){$('#tglr').popover({html : 真的,内容:函数(){返回 $('#customdiv').html();}});});

这是您的示例 https://jsfiddle.net/DTcHh/8529/

您的代码问题之一 - 您执行 $('[data-toggle="popover"]') 它将选择具有等于popover"的数据切换的标签),但您没有这样的标签.如果你想以这种方式初始化弹出框,你应该像这样声明锚点:

<a href="#" data-toggle="popover" class="popover-toggle" title="Popover Header">Toggle popover</a>

但在您的情况下,您只有一个带有一个自定义弹出框的链接,因此更合乎逻辑地按 id 选择它.

I am trying to get separate the bootstrap popover content from the html attributes like you can do with other components, but I can't get it to work...

<!DOCTYPE html>
<html lang="en">
<head>
  <title>Bootstrap Example</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
</head>
<body>

  <div class="container">
    <h3>Popover Example</h3>
    <p>Popovers are not CSS-only plugins, and must therefore be initialized with jQuery: select the specified element and call the popover() method.</p>
    <div class="popover" >
      <a href="#" class="popover-toggle" title="Popover Header" >Toggle popover</a>
        <div class="popover-content">
          Here I am
        </div>
     </div>
  </div>

 <script>
   $(document).ready(function(){
     $('[data-toggle="popover"]').popover();  
   });
 </script>

 </body>
 </html>

解决方案

You have simple anchor and some div which should not be displayed.

<a href="#" id="tglr" class="popover-toggle" title="Popover Header">Toggle popover</a>
<div id="customdiv" style="display: none">
    Here <b>I</b> am
</div>

In JS we get our anchor (by id) and install popover on it with 2 options: first 'html' - allows to display html instead of simple text, second specifies content, which obtained from our div:

$(document).ready(function(){
    $('#tglr').popover({
        html : true, 
        content: function() {
            return $('#customdiv').html();
        } 
    });  
 });

Here is your example https://jsfiddle.net/DTcHh/8529/

One of the issues of your code - you perform $('[data-toggle="popover"]') which will select tags with data-toggle that equals to "popover"), but you have no such tags. If you want to initalize popovers this way you should declare anchor like this:

<a href="#" data-toggle="popover" class="popover-toggle" title="Popover Header">Toggle popover</a>

But in your case you have only one link with one custom popover, so it is more logically select it by id.

这篇关于带有 html 内容的 bootstrap popover的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
其他开发最新文章
热门教程
热门工具
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆