带有 Ajax 的 HTML 链接

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8477190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 11:57:31  来源:igfitidea点击:

HTML link with Ajax

htmlajax

提问by mauguerra

I have 6 different links,and each link is going to call a different Ajax function.

我有 6 个不同的链接,每个链接都将调用不同的 Ajax 函数。

I'm using the <a href>tag because I want it to appear as a link....Can I use this tag to call the Ajax function? or it only works with URL links?

我使用这个<a href>标签是因为我希望它显示为一个链接....我可以使用这个标签来调用 Ajax 函数吗?或者它只适用于 URL 链接?

THANKS!

谢谢!

采纳答案by user1084509

Yes you can incorpore the link in the following way: - on your link you can write <a href="#" onclick="function()">...</a>

是的,您可以通过以下方式合并链接: - 在您的链接上,您可以写 <a href="#" onclick="function()">...</a>

Here you can see further information about this topic:

您可以在此处查看有关此主题的更多信息:

What is the difference between the different methods of putting JavaScript code in an ?

将 JavaScript 代码放入 .

回答by Naterade

This is how I call mine. I give my elements a class name such as 'clickable' then use Jquery's click function as so.

这就是我的称呼。我给我的元素一个类名,例如“可点击”,然后使用 Jquery 的点击功能。

$('.clickable').click(function() {
   //do ajax
});

Then in the function, I get the id of the element as so. var id = this.id, this will get the unique id of the element.

然后在函数中,我得到元素的 id。var id = this.id,这将获得元素的唯一 id。

After that I use the $.postmethod of Jquery, the shorthand version of ajax and complete whatever call you need to make when the user clicks that link using the id.

之后,我使用$.postJquery的方法,即 ajax 的简写版本,并在用户使用 id 单击该链接时完成您需要进行的任何调用。

Of course, in my case I never use the anchor tag, I just make is a button or apply the . clickto the element I wish to add the ajax call to, but you could just surround the "link" in a span or a div to simulate the same effect.

当然,在我的情况下,我从不使用锚标记,我只是制作一个按钮或将 应用. click到我希望将 ajax 调用添加到的元素,但您可以将“链接”括在一个跨度或一个 div 中模拟同样的效果。

Hope this helps in some way or another.

希望这会以某种方式有所帮助。

回答by ianaz

<a href="#" onclick="function()">Text</a>

or even as they wrote, with jquery

甚至在他们写的时候,用 jquery

<a href="#" id="blabla">Text</a>

<script type="text/javascript">
$(document).load(function(){
  $('#blabla').click(function(){
  alert("Clicked");
  });
});
</script>

回答by Gabriele Petrioli

You can extract the value of the hrefattribute and use it for your AJAX call...

您可以提取href属性的值并将其用于 AJAX 调用...

(it is actually the proposed way to handle it..)

这实际上是建议的处理方式..

回答by Punit

yes you can, if any client side script functions (javascript or jquery) applied than it will execute first.

是的,您可以,如果应用了任何客户端脚本函数(javascript 或 jquery),它将首先执行。