如何定义函数以及如何在html中使用每个方法

海森堡

我混淆了如何定义函数以及如何使用它们。

我将each方法应用于 html 表格,但似乎该功能无法正常工作。

我定义了 2 个类,我可以通过单击按钮来选择它们。

在实际情况下,这种处理有些复杂,因此I would like to define click effect as function

① 为什么这个代码不起作用?

② 有没有其他复杂的方法来实现这一点?

如果有人有意见,请告诉我。

谢谢。

$('.click_btn').on('click', function(e) {
  e.preventDefault();
  style = $(this).data().style;
});

 function hospitalization(){
    $(this).nextAll(':lt(3)').addClass(style);
  }

function outpatient() {
      $(this).removeClass().addClass(style);
 }


function register() {
  

  function clicked() {

    
    if (style=="style1"){
    hospitalization()
    }
    
    else{
    outpatient()
    }  
  }

  $(this).on({
    click: clicked
  });
}
$("#calendar .day").each(register);
td {
  padding: 10px;
  border: solid black 1px;
}

table {
  border-collapse: collapse;
}

.is-clicked {
  background-color: aqua;
}

td:hover {
  background-color: yellow;
}

.style1{
background-color:red;}

.style2{
background-color:aqua;}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

<div id=calendar>
  <table>
    <tr>
      <td id=1 class=day>1</td>
      <td id=2 class=day>2</td>
      <td id=3 class=day>3</td>
      <td id=4 class=day>4</td>
      <td id=5 class=day>5</td>
      <td id=6 class=day>6</td>
      <td id=7 class=day>7</td>

    </tr>
  </table>
</div>
<button class="click_btn" data-style="style1">style1</button>
<button class="click_btn" data-style="style2">style2</button>

xdeepakv

你需要稍微清理一下js。global如果您想重用,请定义变量。点击通过$(this)当您直接调用方法时,这将不起作用hospitalization ()

let style = "style1"
$('.click_btn').on('click', function (e) {
    e.preventDefault();
    style = $(this).data().style;
    console.log(style)
});

function hospitalization(elm) {
    elm.nextAll(':lt(3)').addClass(style);
}

function outpatient(elm) {
    elm.removeClass().addClass(style);
}

$("#calendar .day").on("click", function clicked(event) {
    if (style == "style1") {
        hospitalization($(this))
    } else {
        outpatient($(this))
    }
});
td {
  padding: 10px;
  border: solid black 1px;
}

table {
  border-collapse: collapse;
}

.is-clicked {
  background-color: aqua;
}

td:hover {
  background-color: yellow;
}

.style1{
background-color:red;}

.style2{
background-color:aqua;}
<script src="https://code.jquery.com/jquery-3.3.1.js"></script>

<div id=calendar>
  <table>
    <tr>
      <td id=1 class=day>1</td>
      <td id=2 class=day>2</td>
      <td id=3 class=day>3</td>
      <td id=4 class=day>4</td>
      <td id=5 class=day>5</td>
      <td id=6 class=day>6</td>
      <td id=7 class=day>7</td>

    </tr>
  </table>
</div>
<button class="click_btn" data-style="style1">style1</button>
<button class="click_btn" data-style="style2">style2</button>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在循环中使用导入的函数以及该函数内的循环变量?

如何在选择中使用函数以及 Sequalize 中的所有记录?

如何编写自定义函数以显示数据框中每个变量的值计数以及级别?

如何在多重处理函数中传递参数以及如何使用多重处理列表?

如何在HTML JavaScript中调用mfc C ++函数以及如何在mfc C ++中调用JavaScript函数?

为什么我不能使用在 jquery 对象中创建的 javascript 函数以及如何在 jquery 中声明自定义函数?

如何在xargs中使用定义的函数

如何在MYSQL中使用分组依据,计数以及位置

何时以及如何在PostgreSQL中使用窗口函数?

何时以及如何在python中使用内置函数property()

如何检查脚本中是否定义了函数以及如何使用键入的参数执行该函数?

命名空间类,函数以及如何使用

如何在函数中使用append方法?

如何在Ruby中使用map重构每个函数?

如何在用户定义的函数中使用AQL函数?

如何在函数式 API keras 中使用定义的函数

如何在javascript对象中使用构造函数以仅使用`this`中的属性?

如何在 NuxtJs 中创建辅助函数以在 vuex 和组件中使用?

如何在 FLASK 中运行定义的函数以构建 API

如何在 LINQ 中使用自定义聚合函数?(不使用扩展方法)

如何在TypeScript中键入构造函数以及构造函数参数?

如何导出C#dll方法/函数以在C ++中使用它

如何在 Python/Django 的“export”函数中使用先前定义的方法(funcs)?

如何在子组件中使用禁用属性以及如何动态传递函数?

如何在函数中使用用户定义类型?

如何在自定义函数中使用ddply + summary

如何在Excel中使用函数定义下标/上标?

如何在ezplot的函数定义中使用局部常量?

如何在 R.zipWith 中使用预定义函数