1

我有这个 HTML 代码:

<div class="ct-slide" style="left: 55px;">
    <div class="ct-menu-text shadow" style="display: block;">
        <ul>
                <li rel="electronica" data-id="1" title="Electrónica">Electrónica <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="hogar" data-id="3" title="Hogar">Hogar <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="ropa-zapatos-y-accesorios" data-id="4" title="Ropa, Zapatos y Accesorios">Ropa, Zapatos y Accesorios <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="joyas-y-relojes" data-id="5" title="Joyas y Relojes">Joyas y Relojes <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="salud-y-cuidado-personal" data-id="6" title="Salud y Cuidado Personal">Salud y Cuidado Personal <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="deportes-y-actividades-outdoors" data-id="7" title="Deportes y Actividades Outdoors">Deportes y Actividades Outdoors <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
                <li rel="libros" data-id="8" title="Libros">Libros <div class="ct-pointer-cover"></div><div class="ct-pointer"></div></li>
        </ul>
    </div>
        <div rel="electronica" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="hogar" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="ropa-zapatos-y-accesorios" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="joyas-y-relojes" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="salud-y-cuidado-personal" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="deportes-y-actividades-outdoors" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div rel="libros" class="ct-submenu shadow" style="width: 1665px; display: none;">
            ...
        </div>
        <div title="Click para abrir" class="ct-open" style="display: none;"></div>
    <div style="" title="Click para cerrar" class="ct-close"></div>
</div>

正如您所看到的,每个 LI 和 DIV 都具有相同的rel属性,我需要通过此 AJAX 调用在正确的 DIV 元素中编写返回的 HTML,但我无法让它工作。例如,如果我将li rel="electronica"AJAX 调用返回的内容(参见下面的代码)移到应该显示在 中div rel="electronica",如何?有什么帮助吗?

// begin: categories picker
var k = 1;
$(".ct-menu, .ct-menu-text").on("mouseenter", "li", function() {
    var id = $(this).attr('data-id');
    var next = $(this).closest("div[id]").nextAll().length;

    $("#selected_category").attr("value", id);

    if (next > 0) {
        k = k - next;
        $(this).closest("div[id]").nextAll().remove();
    }

    $.ajax({
        type: 'GET',
        url: Routing.generate('category_subcategories', {parent_id: id}),
        dataType: "json",
        success: function(data) {
            if (data.length != 0) {
                $(".ct-submenu").attr('rel').eq(id).append('<div class="product-left" id="cstep' + (k + 1) + '"><ul id="step' + (k + 1) + '"></ul></div>');
                var LIs = "";
                $.each(data, function(index, value) {
                    $.each(value, function(i, v) {
                        LIs += '<li><a class="step ct-sub-menu-text" data-id="' + i + '" href="#">' + v + '</a></li>';
                    })
                });
                $('#step' + (k + 1)).html(LIs);
                k++;
            }
        }
    });
});
// end: categories picker
4

1 回答 1

1

将点击LI的 's保存rel在变量中。然后在选择器中使用该变量来查找匹配的DIV.

当您尝试$(".ct-submenu").attr("rel").eq(0)时,它不起作用,因为.attr("rel")它不是选择器,它是一种返回rel属性内容的方法。

var k = 1;
$(".ct-menu, .ct-menu-text").on("mouseenter", "li", function() {
    var id = $(this).attr('data-id');
    var rel = $(this).attr('rel');
    var next = $(this).closest("div[id]").nextAll().length;

    $("#selected_category").attr("value", id);

    if (next > 0) {
        k = k - next;
        $(this).closest("div[id]").nextAll().remove();
    }

    $.ajax({
        type: 'GET',
        url: Routing.generate('category_subcategories', {parent_id: id}),
        dataType: "json",
        success: function(data) {
            if (data.length != 0) {
                $(".ct-submenu[rel="+rel+"]").append('<div class="product-left" id="cstep' + (k + 1) + '"><ul id="step' + (k + 1) + '"></ul></div>');
                var LIs = "";
                $.each(data, function(index, value) {
                    $.each(value, function(i, v) {
                        LIs += '<li><a class="step ct-sub-menu-text" data-id="' + i + '" href="#">' + v + '</a></li>';
                    })
                });
                $('#step' + (k + 1)).html(LIs);
                k++;
            }
        }
    });
});

正如 Quentin 评论的那样,这并不是对该rel属性的真正适当使用。我建议你给.ct-submenu元素 ID,并将匹配的 ID 放在 LI 的data-submenu属性中。然后你可以这样做:

var rel = $(this).data('submenu');

回调中的选择器将是:

$("#"+rel)
于 2013-09-14T21:28:38.220 回答