javascript - 如何在函数javascript中获取自己的属性

标签 javascript jquery html

我需要获取选择器另一个元素的属性名称 这是我的 html 代码

//I've tried this but it does not work or maybe there is something wrong
/* $(document).ready(function() {
  $('#btnadd').click(function() {
    alert($(this).attr('name'));
  });
}); */

function addval() {
  var btnini=$(this).attr("name");
  $("#txt"+btnini).show();
  $("#btn"+btnini).show();
  $(this).css("display","none");
}
<!-- I've tried this but it does not work or maybe there is something wrong 
//<button type="button" id="btnadd" name="opsional">Tambahkan</button> -->

<input type="text" id="txt-opsional" style="display:none;" />
<button type="button" onClick="addval()" name="opsional">Tambahkan</button>
<button type="button" id="btn-opsional" style="display:none;">simpan</button>

当我单击按钮“tambahkan”时,文本框 id="txt-opsional"和按钮 id="btn-opsional"将显示,因此按钮“tambahkan”将隐藏

希望有更好的建议 谢谢你

最佳答案

由于您使用的是内联事件处理程序(不推荐,尤其是在使用 jQuery 时),您需要传递对被点击元素的引用,您可以通过在内联事件中使用 this 来实现处理程序喜欢

//I've tried this but it does not work or maybe there is something wrong
/* $(document).ready(function() {
  $('#btnadd').click(function() {
    alert($(this).attr('name'));
  });
}); */

function addval(el) {
  var btnini = $(el).attr("name");
  $("#txt-" + btnini).show();//also need to add `-` to txt
  $("#btn-" + btnini).show();
  $(el).css("display", "none");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- I've tried this but it does not work or maybe there is something wrong 
//<button type="button" id="btnadd" name="opsional">Tambahkan</button> -->

<input type="text" id="txt-opsional" style="display:none;" />
<button type="button" onClick="addval(this)" name="opsional">Tambahkan</button>
<button type="button" id="btn-opsional" style="display:none;">simpan</button>


使用 jQuery 处理程序

//I've tried this but it does not work or maybe there is something wrong
$(document).ready(function() {
  $('.addval').click(function() {
    var $this = $(this),
      btnini = $this.attr("name");
    $("#txt-" + btnini).show(); //also need to add `-` to txt
    $("#btn-" + btnini).show();
    $this.hide();
  })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<!-- I've tried this but it does not work or maybe there is something wrong 
//<button type="button" id="btnadd" name="opsional">Tambahkan</button> -->

<input type="text" id="txt-opsional" style="display:none;" />
<button type="button" class="addval" name="opsional">Tambahkan</button>
<button type="button" id="btn-opsional" style="display:none;">simpan</button>

关于javascript - 如何在函数javascript中获取自己的属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31714792/

相关文章:

html - 使用 CSS 设计列表样式

javascript - 如何使用 addEventListener 更改 div 的颜色

php - 在 Jquery 中需要一个 "SEE MORE"风格的侧边栏菜单

Javascript 更改元素的类

jquery - ASP 验证的样式 - RequiredFieldValidator 和 RegularExpressionValidator

html - 单击显示/隐藏仅 div 的 css 且没有 href。没有jquery

javascript - Rx js过滤器方法不返回过滤器数组

javascript - ExpressJS : What's the difference between ? 、 +、 * 在字符串模式和正则表达式中?

javascript - 如何以编程方式向下滚动页面?

html - 如何将 : I would like to have several rectangles (box? ) 自动调整为浏览器的高度