未读取的元素


Elements not being read

我使用json创建了一个动态页面列表,并将其输出到a。由for循环创建的元素似乎无法被JS或CSS读取。例如,作为For循环一部分的所有链接都应该通过jquery-ui变成按钮,但事实并非如此。对话框似乎是在JS提琴工作。现在我已经尝试使用。live,但仍然没有运气。现在再试一次,结果还是一样。这似乎是一个DOM问题,任何帮助将非常感激:)。

jsfiddle联系

http://jsfiddle.net/, togetherjs = 3 qrhr6vf5i

href设置如下

<a id="delete" class="ui-button-text" href="function/variable">Delete</a>

default.js

$(document).ready(function () {
$(function () {
    $.getJSON('webpages/json_data', function (o) {
        for (var i = 0; i < o.length; i++) {
            $('#contentList').append('<div class="contentBox ui-widget-content"><div class="cHeader">' + o[i].page_headline + '</div><div class="cOption"><a href="create/' + o[i].id + '" class="ui-button-text">Edit</a></div><div class="cOption"><a class="ui-button-text" rel="' + o[i].id + '" id="delete" href="json_del">Delete</a></div></div>');
        }
    });
    $("#dialog-confirm").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        height: 180
    });
    $("#delete").on("click", "a", function (event) {
        event.preventDefault();
        var targetUrl = $(this).attr("href");
        var id = $(this).attr("rel");
        var delItem = $(this).parent().parent();
        $("#dialog-confirm").dialog({
            buttons: {
                "Confirm": function () {
                    $.ajax({
                        type: "GET",
                        url: href,
                        data: "id=" + id,
                        dataType: "json",
                        success: function () {
                            delItem.remove();
                        }
                    });
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#dialog-confirm").dialog("open");
    });
});
});

dialog-confirm

<div id="dialog-confirm" title="Delete site page?">
 <p><span class="ui-icon ui-icon-alert" style="float:left; margin:0 7px 20px 0;">  </span>
 This item will be permanently deleted and cannot be recovered. Are you sure?</p>
 </div>`

网页控制器

function json_del()
{
    $id = $this->uri->segment(3);
    $delete = $this->_delete($id);
}
function _delete($id)
{
    $this->load->model('mdl_webpages');
    $this->mdl_webpages->_delete($id);
}

网页模型
function _delete($id)
{
    $table = $this->get_table();
    $this->db->where('id', $id);
    $this->db->delete($table);
}

你的问题是在DOM准备好之前调用js脚本。请将你的JS代码包装成

$(document).ready(function(){
// your code
})

睡了一会儿,做了一些研究后,我需要设置类,然后调用它们。

default.js

$(document).ready(function () {
$(function () {
    $("#dialog-confirm").dialog({
        autoOpen: false,
        modal: true,
        resizable: false,
        height: 300
    });
    $(".delete").click(function (event) {
        event.preventDefault();
        var targetUrl = $(this).attr("href");
        var id = $(this).attr("rel");
        var delItem = $(this).parent().parent();
        $("#dialog-confirm").dialog({
            buttons: {
                "Confirm": function () {
                    $.ajax({
                        type: "GET",
                        url: targetUrl,
                        data: "id=" + id,
                        dataType: "json",
                        success: function () {
                            delItem.remove();
                        }
                    });
                    $(this).dialog("close");
                },
                Cancel: function () {
                    $(this).dialog("close");
                }
            }
        });
        $("#dialog-confirm").dialog("open");
    });
    // setter
    var disabled = $( ".disabled" ).button( "option", "disabled" );
    var delicons = $( ".delete" ).button( "option", "icons" );
    var editicons = $( ".edit" ).button( "option", "icons" );
    var deltext = $( ".delete" ).button( "option", "text" );
    var editext = $( ".edit" ).button( "option", "text" );
    // getter
    $( ".disabled" ).button( "option", "disabled", true );
    $( ".delete" ).button( "option", "icons", { primary: "ui-icon-trash", secondary: null } );
    $( ".edit" ).button( "option", "icons", { primary: "ui-icon-pencil", secondary: null } );
    $( ".delete" ).button( "option", "text", false );
    $( ".edit" ).button( "option", "text", false );
});
});