【问题标题】:Changing the .html of each <td> on click单击时更改每个 <td> 的 .html
【发布时间】:2017-02-26 22:16:43
【问题描述】:

这是我第一次尝试从头开始使用this,我不确定我是否理解正确。我正在尝试将其中包含+&lt;td&gt; 更改为单击时的-,反之亦然。 我没有收到任何错误,我不确定我做错了什么。点击没有任何反应。我在页面上还有其他运行良好的 jquery。

echo "<script type'text/javascript'>
$('.plus_minus').each(function() {
    $(this).click(function(){
        if ($(this).html() == '+'){
            $(this).html = '-';
        }
        else {
            $(this).html = '+';
        }

    })
 });

</script>";

html 看起来像

echo '<tr align="right" class="loc"><td class="plus_minus" width="20" align="center" 
            bordercolor="#000000"
            style="cursor:pointer;font-size:10pt;font-weight:bold;
            border-style:solid;border-width:1pt">+</td>';

任何帮助将不胜感激!

【问题讨论】:

  • 1.打开开发者控制台。 2.$(this).html('-');
  • 您正在尝试为曾经的 .plusminus 类分配一个点击处理程序,这是错误的做法。您应该有一个处理程序来处理所有这些,例如$(.plus_minus).on('click', function(e) { $(this).html ... });
  • @MarcB 我把它改成了$('.plus_minus').on('click', function(e) {,然后我上面的,还是不行

标签: php jquery html this each


【解决方案1】:

$('.plus_minus').each(function() {
   $(this).click(function(){
        if ($(this).text() == '+'){
            $(this).text('-');
        }
        else {
            $(this).text('+');
        }

   })
 });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<table>
<tr align="right" class="loc"><td class="plus_minus" width="20" align="center" 
            bordercolor="#000000"
            style="cursor:pointer;font-size:10pt;font-weight:bold;
            border-style:solid;border-width:1pt">+</td>
</tr>
</table>

【讨论】:

  • 这成功了!我猜是 .html 是罪魁祸首
猜你喜欢
  • 2021-03-16
  • 1970-01-01
  • 1970-01-01
  • 2015-05-04
  • 1970-01-01
  • 2022-01-19
  • 1970-01-01
  • 1970-01-01
  • 2011-01-29
相关资源
最近更新 更多