有没有办法打开 jquery 对话框而不从 div 中获取文本,而是从 javascript 变量中的文本中获取?
问问题
5355 次
1 回答
3
您可以从 javascript 变量中创建和附加<div>
元素,然后使用以下内容将其转换为对话框:
var dialog = "<div id=dialog <h1>Some text</h1></div>";
$('body').append(dialog);
$('#dialog').dialog();
var dialog = "<div id=dialog <h1>Some text</h1></div>";
$('body').append(dialog);
$('#dialog').dialog();
<head>
<title>jQuery UI Dialog - Default functionality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.11.0/jquery-ui.min.js"></script>
<link href="http://code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
</head>
<body>
</body>
您可以使用以下功能自动执行此操作:
var createDialog = function(text , title) {
var dialog = "<div id=dialog <h1>" + text + "</h1></div>";
$('body').append(dialog);
$('#dialog').prop('title' , title);
$('#dialog').dialog();
}
只需调用将变量作为参数传递的函数,即可获得对话框。
// initialize title and body variables
var someVariable = "Some text";
var someTitle = "My title";
var createDialog = function(text , title) {
//create dialog <div> shell
var dialog = "<div id=dialog <h1>" + text + "</h1></div>";
// create the dialog <div>
$('body').append(dialog);
// update the <div>'s title
$('#dialog').prop('title' , title);
//create the dialog
$('#dialog').dialog();
}
createDialog(someVariable , someTitle);
<head>
<title>jQuery UI Dialog - Default functionality</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script>
<script src="http://code.jquery.com/ui/1.10.3/jquery-ui.min.js"></script>
<link rel="stylesheet" type="text/css" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">
</head>
<body>
</body>
于 2013-07-16T02:54:50.447 回答