通过使用“rel”属性定位AJAX会产生正确的DIV

时间:2013-09-14 21:07:02

标签: javascript jquery

我有这个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

1 个答案:

答案 0 :(得分:1)

将点击的LI的{​​{1}}保存在变量中。然后在选择器中使用该变量来查找匹配的rel

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

rel

正如Quentin评论的那样,这并不适合使用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++; } } }); }); 属性。我建议您提供rel元素ID,并将匹配的ID放在LI的.ct-submenu属性中。然后你可以这样做:

data-submenu

并且回调中的选择器将是:

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