简体   繁体   English

在对话框上使用.replaceWith()。 怎么了?

[英]Using .replaceWith() on dialog box. What's wrong?

I would like to toggle the JQuery Dialog using a button. 我想使用一个按钮切换“ jQuery 对话框”

To replace the text in the dialog I use .replaceWith() 要替换对话框中的文本,请使用.replaceWith()

The problem is, that the text isn't replaced, and the dialog shows up, even when I have set 问题是,即使设置了文本,也不会替换文本,并且会显示对话框。

<div id="dialog-confirm" title="Can you confirm?" style="display: none;"> 

Here is the code 这是代码

(function() {
    $( "#dialog:ui-dialog" ).dialog( "destroy" );

    $( "#dialog-confirm" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Okay": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
});

function showhide(iname) {
    var item = document.getElementById(iname).style;
    if (item.display == 'none') {
    item.display = '';
    } else {
    item.display = 'none';
    }
}

$('div.dialog-text').replaceWith( "<b>New text goes here</b>" );

and can be tried out live at 并可以在以下位置现场试用

http://jsfiddle.net/littlesandra88/XxVtU/ http://jsfiddle.net/littlesandra88/XxVtU/

Also, if there is a better way, please let me know =) 另外,如果有更好的方法,请让我知道=)

The text replacement seems to be working in Chrome 11, Firefox 4 and IE 9. The dialog appears because you're initializing the dialog plugin in the document ready event handler and jQuery UI sets new styles (including display ) to the dialog. 文本替换似乎可以在Chrome 11,Firefox 4和IE 9中使用。出现该对话框的原因是,您正在初始化文档就绪事件处理程序中的对话框插件,并且jQuery UI为对话框设置了新样式(包括display )。 If you want it to be hidden, either hide it afterwards or initialize it only when you need it. 如果要隐藏它,则可以事后隐藏它,或者仅在需要时才对其进行初始化。

With it being a modal dialog, the clicking the link to hide it will never work because of the overlay, so you're probably wanting to only ever initialize the dialog box when you need to show it. 由于它是模式对话框,因此单击它的链接以隐藏它将不会起作用,因为它会覆盖,因此您可能只想在需要显示该对话框时对其进行初始化。 To that effect, I moved your initialization code from the ready handler to the showhide function: 为此,我将初始化代码从ready处理程序移至showhide函数:

function showhide(iname) {
    $("#" + iname).dialog({
        resizable: false,
        height: 140,
        modal: true,
        buttons: {
            "Okay": function() {
                $(this).dialog("close");
            },
            Cancel: function() {
                $(this).dialog("close");
            }
        }
    });
}

http://jsfiddle.net/XxVtU/8/ http://jsfiddle.net/XxVtU/8/


As TJ Crowder mentioned in the comments, there's also autoOpen: false , which can be applied during initialization. 正如TJ Crowder在评论中提到的那样,还有autoOpen: false ,它可以在初始化期间应用。 I modified your fiddle to demonstrate this . 我修饰了你的小提琴以证明这一点

what you are doing in you code is saying, 您在代码中所做的工作是说

when the page finishs loading display a dialog box

give your <a> tag an id, and 给您的<a>标记一个ID,然后
you need to add a handler in your initialzation code. 您需要在初始化代码中添加一个处理程序。 Also, you were using anonymous functions, function () { } ,in your document ready call; 另外,您在文档准备调用中使用了匿名函数function () { } which is fine, but it can get kind of cluttery after a while.. 很好,但是过一会儿会变得混乱。

For Example: 例如:

 function init() 
    {
        $("#my_a_tag_id").click(showDialog);
    }

function showDialog() {
    $( "#dialog:ui-dialog" ).dialog( "destroy" );

    $( "#dialog-confirm" ).dialog({
        resizable: false,
        height:140,
        modal: true,
        buttons: {
            "Okay": function() {
                $( this ).dialog( "close" );
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

$(document).ready(init);

If you (for some reason) want to use replace the text using .replaceWith() method, you should make use of the callbacks (in this case the "create" or "open" callback") that are available in jQueryUI. 如果(出于某种原因)要使用.replaceWith()方法替换文本,则应利用jQueryUI中可用的回调(在本例中为“ create”或“ open”回调”)。

I've added an updated version of your own code doing what I thought you were trying to do. 我添加了您自己的代码的更新版本,该版本的功能与我想像的一样。

http://jsfiddle.net/XxVtU/7/ http://jsfiddle.net/XxVtU/7/

take a look at this; 看看这个;

$(document).ready(function(){
 $("#showhide").click(function() {
 $( "#dialog:ui-dialog" ).dialog( "destroy" );

        $( "#dialog-confirm" ).dialog({
            resizable: false,
            height:140,
            modal: true,
            buttons: {
                    "Okay": function() {
                    $( this ).dialog( "close" );
                },
                Cancel: function() {
                    $( this ).dialog( "close" );
                }
            }
        });});$('div.dialog-text').replaceWith( "<b>New text goes here</b>" );});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM