在Ajax中用href替换URL


                    

使用以下代码显示另一页上的文字</ p>

  $ .ajax({
         网址:“ topic / ginger.php”,
         成功:功能(数据){
             数据= $(数据).find('div#display');
             $('。res h2')。html(data);
                         }
     });

               
                    

展开原文

原文

Using following code to display text from another page

    $.ajax({
        url: 'topic/ginger.php',
        success: function (data) {
            data = $(data).find('div#display');
            $('.res h2').html(data);
                        }
    });

               <a href="topic/ginger.php">
                    <div class="res" id="content" style="background-color:rgb(50, 190, 166)">
                        <h2></h2>
                    </div>
                </a>

now i have two div with different href

               <a href="topic/icecre.php">
                    <div class="res"  id="content" style="background-color:rgb(224, 79, 95)">
                        <h2></h2>
                    </div>
                </a>

                <a href="topic/jelly.php">
                    <div class="res" id="content" style="background-color:rgb(92, 184, 92)">
                        <h2></h2>
                    </div>
                </a>

As you see <a href="topic/ginger.php"> is url in ajax code, now i have two other div with different href so different url how do i sole this

1个回答


  $('。res')。each(function(){
     getAjax($(this).find(“ h2”),$(this).parent()。attr('href'));
});

函数getAjax(child,link)
{
    $ .ajax({
         网址:链接,
         成功:功能(数据){
         数据= $(数据).find('div#display');
         child.html(data);
     }
     });
}
</ code> </ pre>

假设您所有的div都在res类中,我认为这可以做到,在res类中循环,并在其中获取h2和其父级的href,并将其传递给您的ajax调用以完成工作。 Havent对此进行了测试。</ p>

EDITL刚刚看到了您的最新编辑,您不能有ID =“ content”的多个项目,因此,请删除它们并为每个类=“ res” </ p>
     </ div>

展开原文

原文

$('.res').each(function(){
    getAjax($(this).find("h2"), $(this).parent().attr('href'));
});

function getAjax(child, link)
{
   $.ajax({
        url: link,
        success: function (data) {
        data = $(data).find('div#display');
        child.html(data);
    }
    });
}

Assuming all your divs are in the res class I would think this would do it, loops throught the res class and grabs the h2 inside it and the href of its parent and passes it to your ajax call to do the work. Havent tested it though.

EDITL just saw your newest edit, you cant have multiple items with the ID = "content" so get rid of them and give them each class ="res"

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐