如何在引导模式中附加html内容

时间:2021-11-21 11:20:46

I want to append html code in bootstrap modal

我想在引导模式中添加html代码

HTML content:

HTML内容:

<div class="modal fade" id="share12" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
   <div class="modal-dialog">
      <div class="modal-content get_direction_modal_bg">
         <div class="text-right"><a class="fa fa-times white normal12" data-dismiss="modal"> close</a></div>
         <div class="modal-body">
            <p class="white text-center">Share this Event</p>
            <div class="share_list_popup">
               <ul>

           <li><a href="https://www.facebook.com/sharer/sharer.php?u=www.10times.com&title=Come join me at"+eventname+"on "+eventstartdate+", "+eventcityname+""></a></li>
                  <li><a href="http://www.linkedin.com/shareArticle?mini=true&utm_campaign=201308&url=http://10times.com/"+eventname+"&title=Come join me at  "+eventname+" on "+eventstartdate+","+eventcityname+".&utm_source=LinkedIn&source=LinkedIn
                     Come join me at "+eventname+" on "+eventstartdate+","+eventcityname+". Find event details at 10times.com/"+eventname+""></a>
                  </li>


               </ul>
            </div>
         </div>
      </div>
   </div>
</div> 
<a href="#" data-target="#share12" data-toggle="modal"><img src="images/share.png"></a>

Javascript code:

Javascript代码:

 var modal_event_name=document.getElementById("modal_event_name");
 var modal_event_startdate=document.getElementById("modal_event_name");
 var modal_city_name=document.getElementById("modal_event_name");
 $('#share12').modal('show');

The problem I'm getting:

我的问题:

  • modal is now working
  • 模态是现在工作
  • guide how to show HTML content in bootstrap modal
  • 指导如何在引导模式中显示HTML内容

JSFiddle: http://jsfiddle.net/4gW4y/115/

JSFiddle:http://jsfiddle.net/4gW4y/115/

5 个解决方案

#1


4  

It looks like you're trying to use Javascript variables (eventname, eventstartdatae, eventcityname) directly in the HTML. Instead, you could try building the URL with Javascript, and set the <li> (list items) and <a href=""> (links) with jQuery:

看起来您正试图在HTML中直接使用Javascript变量(eventname、eventstartdatae、eventcityname)。相反,您可以尝试使用Javascript构建URL,并使用jQuery设置

  • (列表项)和(链接):

  • (列表项)和(链接):
  • <!DOCTYPE html>
    <html>
    
    <head>
      <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    </head>
    
    <body>
    
    <div id="modal_event_name">My Event Name</div>
    <div id="modal_event_startdate">My Event Start Date</div>
    <div id="modal_city_name">My Event City Name</div>
    
    <div class="modal fade" id="share12" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content get_direction_modal_bg">
          <div class="text-right"><a class="fa fa-times white normal12" data-dismiss="modal">close</a></div>
          <div class="modal-body">
            <p class="white text-center">Share this Event</p>
            <div class="share_list_popup">
              <ul>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <button onclick="showModal()">Show Modal</button>
    
    <a href="#" data-target="#share12" data-toggle="modal">Show Modal From Link</a>
    
    <script src="https://code.jquery.com/jquery-1.11.2.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script>
      var eventname = document.getElementById("modal_event_name").textContent;
      var eventstartdate  = document.getElementById("modal_event_startdate").textContent;
      var eventcityname  = document.getElementById("modal_city_name").textContent;
    
      var facebookLink = "https://www.facebook.com/?" + eventname + "&" + eventstartdate + "&" + eventcityname
    
      var linkedinLink = "http://www.linkedin.com/?" + eventname + " & " + eventstartdate + "&" + eventcityname 
    
      $('.share_list_popup ul').append('<li><a href="' + facebookLink + '">Facebook</a></li>');
      $('.share_list_popup ul').append('<li><a href="' + linkedinLink + '">LinkedIn</a></li>');
    
      function showModal() {
        $('#share12').modal('show');
      }
    </script>
    </body>
    
    </html>
    

    Working fiddle

    工作小提琴

    #2


    1  

    Bootstrap comes with methods for showing & hiding modals. With bootstrap 3 you can do:

    Bootstrap提供了显示和隐藏modals的方法。有了bootstrap 3,你可以做:

    $("#pop").on("click", function() { 
      $('#imagemodal').modal('show');
      $('#imagemodal').on('shown.bs.modal', function() {
        $('#imagemodal').find('.modal-body').append('<p>append some html here</p>');
      });
    });
    

    #3


    0  

    Could you use a function to generate the hyperlinks?

    你能用一个函数来生成超链接吗?

    E.g:

    例句:

    <a href="javascript:doSomething()">Link</a>
    
        function doSomething()
        {
        var modal_event_name=document.getElementById("modal_event_name");
         var modal_event_startdate=document.getElementById("modal_event_name");
         var modal_city_name=document.getElementById("modal_event_name");
    
    var url="path?modal_event_name="+modal_event_name+"&modal_event_startdate="+modal_event_startdate + "&modal_city_name="+modal_city_name;
    window.open(url,""."");
        }
    

    Something like this?

    是这样的吗?

    #4


    0  

    The modal is working with the code. Check if bootstrap js is included.

    模式正在处理代码。检查是否包含引导js。

    You can use the shown event of modal to add the variables.

    您可以使用模态的显示事件来添加变量。

    var modal_event_name = "Event Name";
    var modal_event_startdate = "10th july";
    var modal_city_name = "SF"
    $('#share12').modal('show');
    
    $('#share12').on('shown.bs.modal', function () {
        if (!$(this).hasClass('social-links-added')) {
            $(this).addClass('social-links-added');
            var linkedin_url = "http://www.linkedin.com/shareArticle?mini=true&utm_campaign=201308&url=http://10times.com/" + modal_event_name + "&title=Come join me at  " + modal_event_name + " on " + modal_event_startdate + "," + modal_city_name + ".&utm_source=LinkedIn&source=LinkedIn Come join me at " + modal_event_name + " on " + modal_event_startdate + "," + modal_city_name + ". Find event details at 10times.com/" + modal_event_name;
            var facebook_url = "https://www.facebook.com/sharer/sharer.php?u=www.10times.com&title=Come join me at" + modal_event_name + "on " + modal_event_startdate + ", " + modal_city_name;
            $('.share_list_popup ul li:eq(0)').find('a').attr('href', facebook_url);
            $('.share_list_popup ul li:eq(1)').find('a').attr('href', linkedin_url);
            console.log(facebook_url);
            console.log(linkedin_url);
        }
    });
    

    Fiddle

    小提琴

    #5


    0  

    There are several ways to manipulate the data of a modal (or anything else really). Bootstrap provides some, and you can read more about them there.

    有几种方法可以操作一个模态(或者其他任何东西)的数据。Bootstrap提供了一些,您可以在那里了解更多。

    If you want to use jQuery below are some examples:

    下面是一些例子:

    $("#pop").on("click", function() {
    
      //ref. to the modal
      var imgModal = $('#imagemodal');
    
      //Shows the modal
      imgModal.modal('show');
    
      //ref. to the body of the modal
      var imgModalBody = imgModal.find('.modal-body');
    
      //Changes the entire content of the body, in the modal, to this
      imgModalBody.html('<span style="color:red">This is appended directly from a string or variable</span><br/>');
    
      //Appends a button to the modal body
      imgModalBody.append(' <button id="clickMe">Load content from hidden div</button><br/>');
    
      //Adds a click event on the button just appended
      imgModal.on("click", "#clickMe", function() {
    
        //Appends text from the hidden div, myHiddenContent, on the page 
        imgModalBody.append(' ' + $('#myHiddenContent').html());
    
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
    
    <a href="#" id="pop" data-target="#share12" data-toggle="modal">Open my modal</a>
    
    <div id="myHiddenContent" style="display:none;">This is appended from html already on the page (hidden or not)</div>
    
    <!-- Modal -->
    <div class="modal fade" id="imagemodal" 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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Image preview</h4>
          </div>
          <div class="modal-body">
            <img src="" id="imagepreview" style="width: 400px; height: 264px;" />
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>

    #1


    4  

    It looks like you're trying to use Javascript variables (eventname, eventstartdatae, eventcityname) directly in the HTML. Instead, you could try building the URL with Javascript, and set the <li> (list items) and <a href=""> (links) with jQuery:

    看起来您正试图在HTML中直接使用Javascript变量(eventname、eventstartdatae、eventcityname)。相反,您可以尝试使用Javascript构建URL,并使用jQuery设置

  • (列表项)和(链接):

  • (列表项)和(链接):
  • <!DOCTYPE html>
    <html>
    
    <head>
      <link rel="stylesheet" type="text/css" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
    </head>
    
    <body>
    
    <div id="modal_event_name">My Event Name</div>
    <div id="modal_event_startdate">My Event Start Date</div>
    <div id="modal_city_name">My Event City Name</div>
    
    <div class="modal fade" id="share12" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
      <div class="modal-dialog">
        <div class="modal-content get_direction_modal_bg">
          <div class="text-right"><a class="fa fa-times white normal12" data-dismiss="modal">close</a></div>
          <div class="modal-body">
            <p class="white text-center">Share this Event</p>
            <div class="share_list_popup">
              <ul>
              </ul>
            </div>
          </div>
        </div>
      </div>
    </div>
    
    <button onclick="showModal()">Show Modal</button>
    
    <a href="#" data-target="#share12" data-toggle="modal">Show Modal From Link</a>
    
    <script src="https://code.jquery.com/jquery-1.11.2.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
    <script>
      var eventname = document.getElementById("modal_event_name").textContent;
      var eventstartdate  = document.getElementById("modal_event_startdate").textContent;
      var eventcityname  = document.getElementById("modal_city_name").textContent;
    
      var facebookLink = "https://www.facebook.com/?" + eventname + "&" + eventstartdate + "&" + eventcityname
    
      var linkedinLink = "http://www.linkedin.com/?" + eventname + " & " + eventstartdate + "&" + eventcityname 
    
      $('.share_list_popup ul').append('<li><a href="' + facebookLink + '">Facebook</a></li>');
      $('.share_list_popup ul').append('<li><a href="' + linkedinLink + '">LinkedIn</a></li>');
    
      function showModal() {
        $('#share12').modal('show');
      }
    </script>
    </body>
    
    </html>
    

    Working fiddle

    工作小提琴

    #2


    1  

    Bootstrap comes with methods for showing & hiding modals. With bootstrap 3 you can do:

    Bootstrap提供了显示和隐藏modals的方法。有了bootstrap 3,你可以做:

    $("#pop").on("click", function() { 
      $('#imagemodal').modal('show');
      $('#imagemodal').on('shown.bs.modal', function() {
        $('#imagemodal').find('.modal-body').append('<p>append some html here</p>');
      });
    });
    

    #3


    0  

    Could you use a function to generate the hyperlinks?

    你能用一个函数来生成超链接吗?

    E.g:

    例句:

    <a href="javascript:doSomething()">Link</a>
    
        function doSomething()
        {
        var modal_event_name=document.getElementById("modal_event_name");
         var modal_event_startdate=document.getElementById("modal_event_name");
         var modal_city_name=document.getElementById("modal_event_name");
    
    var url="path?modal_event_name="+modal_event_name+"&modal_event_startdate="+modal_event_startdate + "&modal_city_name="+modal_city_name;
    window.open(url,""."");
        }
    

    Something like this?

    是这样的吗?

    #4


    0  

    The modal is working with the code. Check if bootstrap js is included.

    模式正在处理代码。检查是否包含引导js。

    You can use the shown event of modal to add the variables.

    您可以使用模态的显示事件来添加变量。

    var modal_event_name = "Event Name";
    var modal_event_startdate = "10th july";
    var modal_city_name = "SF"
    $('#share12').modal('show');
    
    $('#share12').on('shown.bs.modal', function () {
        if (!$(this).hasClass('social-links-added')) {
            $(this).addClass('social-links-added');
            var linkedin_url = "http://www.linkedin.com/shareArticle?mini=true&utm_campaign=201308&url=http://10times.com/" + modal_event_name + "&title=Come join me at  " + modal_event_name + " on " + modal_event_startdate + "," + modal_city_name + ".&utm_source=LinkedIn&source=LinkedIn Come join me at " + modal_event_name + " on " + modal_event_startdate + "," + modal_city_name + ". Find event details at 10times.com/" + modal_event_name;
            var facebook_url = "https://www.facebook.com/sharer/sharer.php?u=www.10times.com&title=Come join me at" + modal_event_name + "on " + modal_event_startdate + ", " + modal_city_name;
            $('.share_list_popup ul li:eq(0)').find('a').attr('href', facebook_url);
            $('.share_list_popup ul li:eq(1)').find('a').attr('href', linkedin_url);
            console.log(facebook_url);
            console.log(linkedin_url);
        }
    });
    

    Fiddle

    小提琴

    #5


    0  

    There are several ways to manipulate the data of a modal (or anything else really). Bootstrap provides some, and you can read more about them there.

    有几种方法可以操作一个模态(或者其他任何东西)的数据。Bootstrap提供了一些,您可以在那里了解更多。

    If you want to use jQuery below are some examples:

    下面是一些例子:

    $("#pop").on("click", function() {
    
      //ref. to the modal
      var imgModal = $('#imagemodal');
    
      //Shows the modal
      imgModal.modal('show');
    
      //ref. to the body of the modal
      var imgModalBody = imgModal.find('.modal-body');
    
      //Changes the entire content of the body, in the modal, to this
      imgModalBody.html('<span style="color:red">This is appended directly from a string or variable</span><br/>');
    
      //Appends a button to the modal body
      imgModalBody.append(' <button id="clickMe">Load content from hidden div</button><br/>');
    
      //Adds a click event on the button just appended
      imgModal.on("click", "#clickMe", function() {
    
        //Appends text from the hidden div, myHiddenContent, on the page 
        imgModalBody.append(' ' + $('#myHiddenContent').html());
    
      });
    
    });
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js"></script>
    <link href="http://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.min.css" rel="stylesheet" />
    
    <a href="#" id="pop" data-target="#share12" data-toggle="modal">Open my modal</a>
    
    <div id="myHiddenContent" style="display:none;">This is appended from html already on the page (hidden or not)</div>
    
    <!-- Modal -->
    <div class="modal fade" id="imagemodal" 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"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span>
            </button>
            <h4 class="modal-title" id="myModalLabel">Image preview</h4>
          </div>
          <div class="modal-body">
            <img src="" id="imagepreview" style="width: 400px; height: 264px;" />
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
        </div>
      </div>
    </div>