English 中文(简体)
如何以动态方式在jquery 中呼叫 URL?
原标题:how to call url dynamically in jquery?
  • 时间:2012-05-23 06:04:56
  •  标签:
  • jquery
  • ajax

我有一个像这样的函数:

function contentDisp()
{
$.ajax({
url : "../Patient/brest/ros_brest.php",
success : function (data) {
$("#dynamicContent").html(data);
}
});
}

我有这样的菜单:

<div  id="leftmenu">
<ul id="accordion">
    <li><div>BREAST</div>
        <ul>
            <li ><a href="#" onClick="contentDisp();">BREAST</a></li>
                    </ul>

当终端用户点击菜单项时, 相关页面将显示为 div 分配的 div 中 。 一切正常, 但是现在如果我想对所有菜单项都做同样的事情, 那么我要怎么处理这个 URL : "./ Patitent/ brest/ ros_ brest. php ", 而不是 URL 静态地如何动态传递它 。

Thank you in advance ramsai

最佳回答
    function contentDisp(url)
{
$.ajax({
url : url,
success : function (data) {
$("#dynamicContent").html(data);
}
});
}

<li ><a href="#" onClick="contentDisp("../Patient/brest/ros_brest.php");">BREAST</a></li>

However, What I would suggest is you remove the onClick handlers and put the url as the href that way with js off users still get somethign (read about progressive enhancement to learn more) like so

$("#accordion>a").click(function()
{
$.ajax({
url : $(this).attr("href"),
success : function (data) {
$("#dynamicContent").html(data);
}
});
 return false;
});

Demo:http://jsbin.com/ixifah/4" rel=“no follow'>http://jsbin.com/ixifah/4

问题回答

类似的东西:

<script type="application/javascript">
$(document).ready(function() {
   $("a.dyn-load").on("click", function() {
      $("#" + $(this).attr("data-target")).load($(this).attr("href"));
      return false;
   });
});
</script>

<a href="/some/url" class="dyn-load" data-target="dyn-content">bleh</a>
<a href="/some/url2" class="dyn-load" data-target="dyn-content">bleh2</a>
<div id="dyn-content"></div>
<a href="/some/url3" class="dyn-load" data-target="dyn-content2">bleh3</a>
<a href="/some/url4" class="dyn-load" data-target="dyn-content2">bleh4</a>
<div id="dyn-content2"></div>

"http://jsfiddle.net/Regisc/RnMJN/" rel="nofollow" >Demo

你可以做到这一点:

<a href="some_page_url" onClick="contentDisp(this);">BREAST</a>

function contentDisp(obj) {
  var someurl = $(obj).attr("href");
  $.ajax({
        url : someurl,
        success : function (data) {
            $("#dynamicContent").html(data);
        }
  });
}




相关问题
ajax login using httpRequest?

I am trying to develop my login script to give feedback to the user if the login is valid or not. Basically if it isn t correct a div box will show saying its wrong, if its correct it will show its ...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

How can i update div continuously

I have asp.net application where i have a div which showing the value from other site. The value of that site is changing continuously. I want that my div will automatically update in some interval ...

热门标签