将click事件添加到主菜单中的所有"a"标记

apo*_*pse 4 javascript ajax jquery

$(document).ready(function () {
            $("#MainMenu a").click(function () {
                $("#divContent").load( ???? );
            });
        });
Run Code Online (Sandbox Code Playgroud)

我想从我的主菜单中检索所有链接,将click事件附加到它们,并告诉jQuery #divContent通过ajax调用加载一些内容.内容位置应取决于href每个链接中的标记.

PSL*_*PSL 8

你快到了,试试:

 $("#MainMenu a").click(function (e) {
     e.preventDefault(); //prevent the default click behavior of the anchor.
     $("#divContent").load(this.href); //just get the href of the anchor tag and feed it onto load.
 });
Run Code Online (Sandbox Code Playgroud)

  • 不要忘记`event.preventDefault()`. (2认同)