2010-02-28 107 views
10

我想這一點:jquery模態對話框onclick?

http://jqueryui.com/demos/dialog/#modal-message

當你點擊ClickMe到happend。

該怎麼辦?

<script type="text/javascript"> 
$(document).ready(function() { 
$('div.thedialog').dialog({ autoOpen: false }) 
$('#thelink').click(function(){ $('div.thedialog').dialog('open'); }); 
} 
    </script> 
</head> 
<body> 
<div id="thedialog" title="Download complete"> 
    <p> 
     <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span> 
     Your files have downloaded successfully into the My Downloads folder. 
    </p> 
    <p> 
     Currently using <b>36% of your storage space</b>. 
    </p> 
</div> 
<a href="#" id="thelink">Clickme</a> 

什麼happends

回答

13

而不是div.thedialog,給div#thedialog.與類名稱一起使用,當您使用ID時使用#

(另外,如果這是你實際使用的代碼,有一個失蹤支架:))

工作代碼:

<!doctype html> 
<head> 
    <link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.0/themes/ui-lightness/jquery-ui.css" /> 
    <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script> 
    <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.2/jquery-ui.min.js" type="text/javascript"></script> 
    <script type="text/javascript"> 
$(document).ready(function() { 
$('div#thedialog').dialog({ autoOpen: false }) 
$('#thelink').click(function(){ $('div#thedialog').dialog('open'); }); 
}) 
    </script> 
</head> 
<body> 
<div id="thedialog" title="Download complete"> 
    <p> 
     <span class="ui-icon ui-icon-circle-check" style="float:left; margin:0 7px 50px 0;"></span> 
     Your files have downloaded successfully into the My Downloads folder. 
    </p> 
    <p> 
     Currently using <b>36% of your storage space</b>. 
    </p> 
</div> 
<a href="#" id="thelink">Clickme</a> 
</body> 
+1

+1只是不要忘記包含樣式表,它的樣子更好:) – 2010-02-28 23:52:47

+0

@Alex更新了樣式表的答案。 – madaboutcode 2010-03-01 00:13:39

0

使用jQuery UI對話框,在$(document).ready(...)做:

$('div.thedialog').dialog({ autoOpen: false }) 

創建對話框和

$('#thelink').click(function(){ $('div.thedialog').dialog('open'); }); 

將其打開。

+0

不工作..我有這樣的: Clickme,然後這兩行文檔準備好了,然後是一個名爲「thedialog」的div框,但它不會工作 – Karem 2010-02-28 23:12:13

0

@Azzyh我覺得@Rune意味着你必須爲它創建一個腳本。

你把這個到HTML的標籤

<script src="script.js" type="text/javascript"></script> 

(你也必須有jQuery的UI腳本和jQuery腳本也鏈接到你的頁面,上面如鋸(前 <script src="http://jquery-ui.googlecode.com/svn/tags/latest/ui/minified/jquery-ui.min.js" type="text/javascript"></script>)< - 在女巫的情況下,他加載互聯網腳本)。

其中script.js是腳本文件(與html文件位於同一文件夾中)。

現在,在你的script.js寫

$(document).ready(function(){ 
    $('div.thedialog').dialog({ autoOpen: false }) 
    $('#thelink').click(function(){ $('div.thedialog').dialog('open'); }); 
}); 

PS:讀this book,如果你想學習如何做你在互聯網上看到所有這些很酷的東西。