[英]How to target an id or class inside html using javascript?
I came across with a problem that both "button" tag inside my page are being affected with the toggle.js dropdown effect. 我遇到一个问题,就是页面中的两个“按钮”标签都受toggle.js下拉效果的影响。 So i decided to add an id to the first one so i can just call it in "toggle.js" the problem now is i wasn't sure which one should i change inside the code(js)?
所以我决定在第一个ID上添加一个ID,这样我就可以在“ toggle.js”中调用它了。现在的问题是我不确定在code(js)中应该更改哪个ID? anyone knows how to target a button with id?
有谁知道如何以id为目标的按钮?
Here is the toggle.js 这是toggle.js
$(function () {
img_arrow = '<img src="images/down-arrow.png" width="20" height="20" alt="" style="vertical-align:middle; padding-top: 0px;margin-left:2px"/>';
var $window = $(window),
$nav = $('nav'),
$button = $('button');
$button.on('click', function () {
$nav.slideToggle();
});
$window.on('resize', function () {
if ($window.width() > 320) {
$nav.show();
}
});
$('#menu a').click(function (e) {
$('button').html($(this).html() + img_arrow);
e.preventDefault();
});
});
Thank you in advance! 先感谢您!
It would be $("button#ID").on('click', function () ....
这将是
$("button#ID").on('click', function () ....
Edit: 编辑:
According to your fiddle, your button has a CLASS attribute, not ID. 根据您的小提琴,您的按钮具有CLASS属性,而不是ID。 So the above would be
$("button.submitButton").on('click' ...
所以上面是
$("button.submitButton").on('click' ...
Or like commented below, you can simply have it as $(".submitButton")...
, but I like to be accurate, just incase you have multiple elements with same class value. 或如下所述,您可以将其简单地设置为
$(".submitButton")...
,但是我想准确地说,以防万一您有多个具有相同类值的元素。
Use css syntax to select items using jQuery. 使用CSS语法使用jQuery选择项目。 Try
$("#yourID").on('click', function())....
尝试
$("#yourID").on('click', function())....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.