jQuery Mobile улучшает разметку на вашей странице, но вам даже не нужно точно знать, какая именно иерархия DOM-Tree. В представленной демонстрации вам нужно найти первый collapsible
над кнопкой Remove
.
Вот оно:
$(document).on("vclick", ".removeStyleBtn", function() {
var parentCollapsible = $(this).closest('div[data-role="collapsible"]');
var parentCollapsibleSet = $(parentCollapsible).closest('div[data-role="collapsibleset"]');
$(parentCollapsible).collapsible("destroy");
$(parentCollapsible).remove();
$(parentCollapsibleSet).collapsibleset("refresh");
});
Кстати, я бы посоветовал Вам также отфильтровать событие pagecreate
только для страницы, которая содержит набор разборных операций (или использовать оператор switch
), в противном случае - если у вас более одной страницы - код будет исполняться более одного раза. В вашем примере примерно так:
Markup:
<div data-role="page" id="page-with-collapsible">
JS:
$(document).on("pagecreate", "#page-with-collapsible", function() {
var nextId = 1;
$("#add").click(function() {
nextId++;
var content = "";
content += "<div data-role='collapsible' id='set" + nextId + "'>";
content += "<h3>Section " + nextId + "</h3>";
content += "<p>I am the collapsible content in a set so this feels like an accordion. I am hidden by default because I have the 'collapsed' state; you need to expand the header to see me.</p>";
content += "<button id='removeStyle" + nextId + "' class='removeStyleBtn ui-btn ui-btn-b ui-shadow ui-corner-all ui-mini'>Remove</button></div>";
$("#set").append(content).collapsibleset("refresh");
$("#set" + nextId).collapsible("expand");
console.log("collapsible set " + nextId + " added !!!");
});
});