[英]cant combine text and image into JQuery Dialog box
I am trying to add a picture and text to my JQuery Dialog box yet I am having problems, I'm new to JQuery but can't seem to find anything wrong with the code. 我试图在我的JQuery对话框中添加图片和文本,但我遇到了问题,我是JQuery的新手,但似乎无法找到代码的任何问题。 Anyone have any ideas on what the problem is ? 任何人对问题是什么都有任何想法?
$(function() {
$( ".dialog" ).click(function(){
$('#dialog').append('<img src="Image here"/><br/>').append($(this).html());
$('#dialog').html("The Football Game ") + $(this).html() + " Is now playing")dialog({
resizable:true,
modal:true,
height: 350,
width: 500,
buttons: {
"Ok": function()
{
$(this).dialog('close');
},
"Cancel": function()
{
$(this).dialog('close');
}
}
});
});
});
Using your very example, slightly rewritten. 使用你的例子,略有改写。
HTML markup to trigger dialog: 用于触发对话框的HTML标记:
<div id="dialog" title="Basic dialog">
<p>Here's some content</p>
</div>
<!-- a HTML element with the class "dialog" to trigger the event -->
<input type="button" class="dialog" value="Trigger Dialog"/>
Your jQuery example modified: 您的jQuery示例已修改:
$(document).ready(function(){
$('.dialog').click(function(){ //use a better selector to trigger this event..
var fetchHTML = $('#dialog').html(); //fetch the current HTML of your element with the ID "dialog" and save it within a variable
$('#dialog').html('<img alt="" src="someimage.png"/>'+ fetchHTML); // add your image to the element and then the HTML that was there before. The method htm() will overwrite everything that was there before
$('#dialog').dialog({ //use the jquery-ui dialog method with selected parameters
resizable : true,
modal : true,
height : 350,
width : 500,
buttons: {
"Ok": function(){
$(this).dialog('close');
},
"Cancel": function(){
$(this).dialog('close');
}
}
});
});
});
And remember to make sure that you have successfully included: 并记住要确保您已成功包括:
Google hosts them all for you https://developers.google.com/speed/libraries/devguide . Google会为您托管所有内容https://developers.google.com/speed/libraries/devguide 。 Easy as pie to just copy and paste them within your document 只需将它们复制并粘贴到您的文档中即可
Whenever developing web applications I use FireFox and have installed extensions such as FireBug, WebDeveloper and YSlow. 每当开发Web应用程序时,我都使用FireFox并安装了扩展程序,如FireBug,WebDeveloper和YSlow。 They're of great help :) Just saying... 他们有很大的帮助:)只是说......
The main issue I see is in the line below: 我看到的主要问题如下:
$('#dialog').html("The Football Game ") + $(this).html() + " Is now playing")dialog({
There is an extra )
which is closing the html()
and you are missing a .
有一个额外的)
关闭html()
,你错过了一个.
before calling dialog()
. 在调用dialog()
之前。
$('#dialog').html("The Football Game " + $(this).html() + " Is now playing").dialog({
Also, you are calling html()
after append()
, essentially overwriting the appended content. 此外,您在 append()
之后调用html()
append()
,基本上覆盖了附加的内容。 Try using append()
after html()
(or combine all the content into the html()
call): 尝试在html()
append()
之后使用append()
(或将所有内容组合到html()
调用中):
$('#dialog')
.html('<img src="http://dummyimage.com/600x400/000/fff"/><br/>')
.append("The Football Game "+ $(this).html() + " Is now playing")
.dialog({ ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.