用JS引导程序,弹出窗口中的函数结果

时间:2014-11-12 06:08:12

标签: javascript html twitter-bootstrap

这是从天气API获取伦敦温度的代码。它工作正常(图像是本地的,因此不会显示):

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8" />
        <meta name="format-detection" content="telephone=no" />
        <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
        <link rel="stylesheet" type="text/css" href="css/body.css" />

        <meta name="msapplication-tap-highlight" content="no" />
    </head>
    <body>
        <script src="http://code.jquery.com/jquery-2.0.0.js"></script>
        <script language="javascript" type="text/javascript">
        <!--
            function foo(callback) {
                $.ajax({
                url: "http://api.openweathermap.org/data/2.5/weather?q=London",
                dataType: 'JSON',
                success: callback
                });
            }

            function myCallback(result) {
                var temp = JSON.stringify(JSON.parse(result.main.temp));
                var Kelvin = 272;
                var Centigrade = Math.round(temp-Kelvin);

                if (Centigrade <= 25) {
                    //alert("Temperature : "+Math.round(Centigrade)+" C");
                    var temp = document.getElementById("temp");
                    temp.style.fontSize = "20px";
                    temp.innerHTML = Centigrade+"° C , Cool&nbsp;&nbsp;&nbsp;"+"<img src= \"img/Tlogo2.svg\"/>";
                    //document.getElementById("temp").innerHTML = Centigrade+"° C , Cool&nbsp;&nbsp;&nbsp;"+"<img src= \"img/Tlogo2.svg\"/>";
                }
                else if (Centigrade > 25) {
                    var temp = document.getElementById("temp");
                    temp.style.fontSize = "20px";
                    temp.innerHTML = Centigrade+"° C , Cool&nbsp;&nbsp;&nbsp;"+"<img src= \"img/Tlogo3.svg\"/>";
                    //document.getElementById("temp").innerHTML = Centigrade+"° C , It's Hot !!! "+"<img src= \"img/Tlogo3.svg\"/>";
                }
            }
        </script>

        <div style="position: absolute; left: 30px; top: 75px;"> 
            <img src="img/temlogo.svg" width="35" height="35" onclick="foo(myCallback);"/>
        </div>

        <p id="temp"></p>
    </body>
</html>

从教程点和Bootstrap网站我试图使用一个可以忽略的popover。它也可以正常工作:

<!DOCTYPE html>
<html>

    <body>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://code.jquery.com/jquery-2.0.0.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        <script language="javascript" type="text/javascript">
            $(function() {
                $("[data-toggle='popover']").popover();
            });
        </script>
    </body>
    <a href="#" tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Temperature" data-content="40C">Temperature</a>
</html>

现在我正在尝试的是我想要温度作为弹出元素。即。如果我点击图像按钮,它应该触发温度获取功能,然后显示温度和与弹出框相关的图像。所以这是我面临的两个挑战。

  1. 设置图像而不是红色按钮,然后设置温度数据
  2. 列出项目和图像即。 Tlogo2.svg出现在那个弹出框中。
  3. 那么有人可以建议如何设置吗?

    编辑:我试过这个来实现我所说的。但什么都没发生。代码在这里:

    <!DOCTYPE html>
    <html>
    
    <body>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://code.jquery.com/jquery-2.0.0.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        <script language="javascript" type="text/javascript">
    
    //Function
    function foo(callback) {
        $.ajax({
        url: "http://api.openweathermap.org/data/2.5/weather?q=London",
        dataType: 'JSON',
        success: callback
        });
    }
    
    function myCallback(result) {
    var temp = JSON.stringify(JSON.parse(result.main.temp));
    var Kelvin = 272;
    var Centigrade = temp-Kelvin;
    alert("Temperature : "+Math.round(Centigrade)+" C");
    //document.getElementById("temp").innerHTML = "Temperature : "+Math.round(Centigrade)+" C";
    }
    
            $(function() {
                $("[data-toggle='popover']").popover(myCallback(result));
            });
        </script>
    </body>
    <a href="#" tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Temperature" data-content="40C">Temperature</a>
    
    </html>
    

    我正在添加一些补充。因此人们不会混淆并看到我真正想要的东西。我想要函数的结果,即温度是23 C到弹出元素 代码:

    <!DOCTYPE html>
    <html>
    
    <body>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <script src="http://code.jquery.com/jquery-2.0.0.js"></script>
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css">
        <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap-theme.min.css">
        <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.0/js/bootstrap.min.js"></script>
        <script language="javascript" type="text/javascript">
    
    //Function
    
    function foo(callback) {
        $.ajax({
        url: "http://api.openweathermap.org/data/2.5/weather?q=London",
        dataType: 'JSON',
        success: callback
        });
    }
    
    function myCallback(result) {
    var temp = JSON.stringify(JSON.parse(result.main.temp));
    var Kelvin = 272;
    var Centigrade = temp-Kelvin;
    alert("Temperature : "+Math.round(Centigrade)+" C");
    //document.getElementById("temp").innerHTML = "Temperature : "+Math.round(Centigrade)+" C";
    }
    
    
    $(function() {
                    $("[data-toggle='popover']").popover(myCallback);
                });
        </script>
    </body>
    <a href="#" tabindex="0" class="btn btn-lg btn-danger" role="button" data-toggle="popover" data-trigger="focus" title="Temperature" data-content= "myCallback(result);" >Temperature</a>
    
    </html>
    

    让我知道我需要改变的地方。

1 个答案:

答案 0 :(得分:0)

也许你可以在悬停时做popover,这是示例

  $(function() {
      $('[title]').attr("data-rel", "tooltip");
      $("[data-rel='tooltip']")
          .attr("data-placement", "top")
          .attr("data-content", function() {
              return $(this).attr("title")
          })
          .removeAttr('title');


      var showPopover = function() {
          $(this).popover('show');
      };
      var hidePopover = function() {
          $(this).popover('hide');
      };
      $("[data-rel='tooltip']").popover({
          trigger: 'manual'
      }).click(showPopover).hover(showPopover, hidePopover);

  });

像这样使用

 <a href="#" tabindex="0" class="btn btn-lg btn-danger" title="40c">Temperature</a>