jQuery 使用jquery点击跨度

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13493850/
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-26 12:40:38  来源:igfitidea点击:

onclick on span using jquery

jquery

提问by Ayman Hussein

I have the following HTML:

我有以下 HTML:

<a title="Prev" data-event="click" data-handler="prev" class="ui-datepicker-prev ui-corner-all">
       <span class="ui-icon ui-icon-circle-triangle-w">Prev</span>
   </a>

I need to change 'Prev' once click on span using jquery.

一旦使用 jquery 单击跨度,我需要更改“上一个”。

This is my code:

这是我的代码:

$(".ui-icon.ui-icon-circle-triangle-w").click(function(){ 
    $(".ui-icon.ui-icon-circle-triangle-w").html('?');         
});

But it does not work, why?

但它不起作用,为什么?

回答by Magus

Maybe your code is executed too early. And your html element just doesn't exist when your code is executed. So $(".ui-icon.ui-icon-circle-triangle-w")is just empty.

也许你的代码执行得太早了。当您的代码执行时,您的 html 元素不存在。所以$(".ui-icon.ui-icon-circle-triangle-w")只是空的。

You can use this :

你可以使用这个:

$("body").on('click', ".ui-icon.ui-icon-circle-triangle-w", function(){ 
    $(this).html('?');         
});

回答by Munchies

Give your <span>a class:

给你<span>上课:

<a title="Prev" data-event="click" data-handler="prev" class="ui-datepicker-prev ui-corner-all">
       <span class="prevSpan ui-icon ui-icon-circle-triangle-w">Prev</span></a>

and then use it via jQuery:

然后通过 jQuery 使用它:

$(".prevSpan").text("?");

You need a eventhandler for the click event, use .on()
finally the code:

您需要单击事件的事件处理程序,.on()
最后使用代码:

$(document).ready(function(){
  $('.prevSpan').on('click',function(){
    $(".prevSpan").text("?");
  });
});

Documentation for the .on() eventhandler http://api.jquery.com/on/

.on() 事件处理程序的文档http://api.jquery.com/on/