有没有办法打开一个jquery对话框而不从div中获取文本,而是从javascript变量中的文本中获取文本?
答案 0 :(得分: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>