使用bootstrap popover在其中显示结果

时间:2014-11-13 06:01:49

标签: javascript jquery 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进行一些很好的可视化:

<!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>

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

  • 设置图像而不是红色按钮,然后设置温度数据

  • 列出项目和图像即。 Tlogo2.svg将出现在那个流行音乐中 过盒子。

那么有人可以建议如何设置吗?

有关更多说明,我正在添加其他代码。这里的数据内容显示了函数名称。但我想要函数的结果:

<!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>

请帮帮我。

2 个答案:

答案 0 :(得分:0)

使用Popover的选项来美化你的弹出窗口。使用这些选项,您可以完全控制弹出窗口的功能和外观。

http://getbootstrap.com/javascript/#popovers-usage

This fiddle只展示了其中一些选项...

Js代码

$(document).ready(function(){
        $(".popover-examples a").popover({
            title : 'Weather App',
            trigger: 'hover',
            template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div><img src="http://lorempixel.com/100/100" /><span>Just an example of inserting image in a Popover...</span></div>'
        });
    });

<强> HTML

<div class="bs-example">
    <p class="popover-examples">
        <a href="#" class="btn btn-lg btn-primary" data-toggle="popover" >Popover Example</a>
    </p>    
</div>

答案 1 :(得分:0)

结帐demo

function foo() {
    $.ajax({
        url: "http://api.openweathermap.org/data/2.5/weather?q=London",
        dataType: 'JSON',
        success: function(result) {
            var temp = JSON.stringify(JSON.parse(result.main.temp));
            var Kelvin = 272;
            var Centigrade = temp-Kelvin;
            var temperature = "Temperature : "+Math.round(Centigrade)+" C";
            $('span.temp-val').text(temperature);
        }
    });
}

$(document).ready(function(){
    $(".popover-examples a").popover({
        title : 'Weather App',
        trigger: 'hover',
        template: '<div class="popover"><div class="arrow"></div><h3 class="popover-title"></h3><div class="popover-content"></div><img src="http://lorempixel.com/100/100" /><span class="temp-val">Just an example of inserting image in a Popover...</span></div>'
    });

    $(".popover-examples a").hover(function() { foo(); })
});

您需要在悬停触发元素时调用ajax函数。另外,在模板中设置温度标识符 - 这样您就可以使用ajax调用将其设置为获得的值。