0

我正在用 JQTouch 编写一个应用程序,并且正在使用一个红色的大按钮

<a href="#" class="redButton">Red</a>

我正在使用 PHP 动态构建具有多个 div 的 JQT 页面。该应用程序是一个从 MySQL 获取数据的服务器管理控制台。我的想法是我使用 While 循环为 MySQL 查询中返回的每个服务器创建一个 div,每个 div 将有一个删除服务器按钮(大红色按钮)。由于整个动态页面生成的东西,我不得不调用代码的 dame 位。所以我想知道是否有一种方法可以让我使用按钮 Red 调用的 onClick 函数知道按钮所在的 div 正在调用该函数。多个 div 中将有一个按钮调用相同的代码,但我必须知道要删除的服务器。有什么建议么?

这是完整的源代码。

<html>    
<link rel="stylesheet" href="jq_touch/themes/css/jqtouch.css" title="jQTouch">
<script src="jq_touch/src/lib/zepto.min.js" type="text/javascript" charset="utf-8"></script>
<script src="jq_touch/src/jqtouch.min.js" type="text/javascript" charset="utf-8"></script>
<!-- Uncomment the following two lines (and comment out the previous two) to use jQuery instead of Zepto. -->
<!-- <script src="../../src/lib/jquery-1.7.min.js" type="application/x-javascript" charset="utf-8"></script> -->
<!-- <script src="../../src/jqtouch-jquery.min.js" type="application/x-javascript" charset="utf-8"></script> -->
<script src="../../extensions/jqt.themeswitcher.min.js" type="application/x-javascript" charset="utf-8"></script>
<script type="text/javascript" charset="utf-8">
var jQT = new $.jQTouch({
icon: 'jqtouch.png',
icon4: 'jqtouch4.png',
addGlossToIcon: false,
startupScreen: 'jqt_startup.png',
                statusBar: 'black-translucent',
                themeSelectionSelector: '#jqt #themes ul',
                preloadImages: []
            });

            // Some sample Javascript functions:
            $(function(){

                // Show a swipe event on swipe test
                $('#swipeme').swipe(function(evt, data) {
                    var details = !data ? '': '<strong>' + data.direction + '/' + data.deltaX +':' + data.deltaY + '</strong>!';
                    $(this).html('You swiped ' + details );
                    $(this).parent().after('<li>swiped!</li>')
                });

                $('#tapme').tap(function(){
                    $(this).parent().after('<li>tapped!</li>')
                });

                $('a[target="_blank"]').bind('click', function() {
                    if (confirm('This link opens in a new window.')) {
                        return true;
                    } else {
                        return false;
                    }
                });

                // Page animation callback events
                $('#pageevents').
                    bind('pageAnimationStart', function(e, info){ 
                        $(this).find('.info').append('Started animating ' + info.direction + '&hellip;  And the link ' +
                            'had this custom data: ' + $(this).data('referrer').data('custom') + '<br>');
                    }).
                    bind('pageAnimationEnd', function(e, info){
                        $(this).find('.info').append('Finished animating ' + info.direction + '.<br><br>');

                    });

                // Page animations end with AJAX callback event, example 1 (load remote HTML only first time)
                $('#callback').bind('pageAnimationEnd', function(e, info){
                    // Make sure the data hasn't already been loaded (we'll set 'loaded' to true a couple lines further down)
                    if (!$(this).data('loaded')) {
                        // Append a placeholder in case the remote HTML takes its sweet time making it back
                        // Then, overwrite the "Loading" placeholder text with the remote HTML
                        $(this).append($('<div>Loading</div>').load('ajax.html .info', function() {        
                            // Set the 'loaded' var to true so we know not to reload
                            // the HTML next time the #callback div animation ends
                            $(this).parent().data('loaded', true);  
                        }));
                    }
                });
                // Orientation callback event
                $('#jqt').bind('turn', function(e, data){
                    $('#orient').html('Orientation: ' + data.orientation);
                });

            });
        </script><?php
//Connect
mysql_connect("localhost", "root", "root") or die(mysql_error());
//Make and store queries
mysql_select_db("servermgr") or die(mysql_error());
$result = mysql_query("SELECT * FROM servers") 
or die(mysql_error());


//Echo some constant HTML
echo'<div id="serverset">';
echo'<div class="toolbar">';
echo'<h1>Servers Home</h1> ';
echo'</div>';


echo'<ul class="rounded">';


//Begin printing out MYSQL rows (List Items)
while($row = mysql_fetch_array( $result )) {
//$row_friendlyName = $_row['friendly_name']
$friendlyName_noSpaces = str_replace(' ', '_', $row[friendly_name]);
echo'<li class=""><a href="#'.$friendlyName_noSpaces.'">'.$row["friendly_name"].'</a></li>';    

}


//Close list
echo'</ul>';
echo '</div>';


//Redo all previous queries to print out the divs
mysql_select_db("servermgr") or die(mysql_error());

$result2 = mysql_query("SELECT * FROM servers") 
or die(mysql_error());
while($row2 = mysql_fetch_array( $result2 )) {
    $friendlyName_noSpaces2 = str_replace(' ', '_', $row2[friendly_name]);
echo '<div id="'.$friendlyName_noSpaces2.'">';


echo'<div class="toolbar">';
echo'<h1>'.$row2[friendly_name].'</h1> ';
echo '<a href="#" class="back">Back</a>';
echo'</div>';
echo'<ul class="rounded">';
echo '<li>Friendly Name: '.$row2[friendly_name].'</li>';
echo '<li>IP Address: '.$row2[ip].'</li>';
echo '<li>Server Hostname: '.$row2[hostname].'</li>';
echo '<li>MAC Address: '.$row2[MAC].'</li>';
echo'</ul>';
echo'<button href="#" class="redButton">Red</button>';





echo'</div>';
}
//END OF PHP
?>
        </body>


        </html>
4

2 回答 2

1

DevZer0 的答案可能是您想要使用的,但另一种方法是将一个类添加到包含的 div 中,例如

echo '<div id="'.$friendlyName_noSpaces2.'" class="server">';

然后你可以在你的回调中做到这一点

var server = $(this).closest(".server").attr("id");

获取包含 div 的 id。

于 2013-07-11T03:41:29.553 回答
1

如下向您的“大红色按钮”添加数据属性

<a href="#" class="redButton" data-server="serverval">Red</a>

并从您的处理代码中检索数据值,如下所示

var server = $(this).attr('data-server');

然后你可以做你的条件逻辑。

于 2013-07-11T03:22:27.790 回答