简体   繁体   English

如何使用JavaScript定位HTML内的ID或类?

[英]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为目标的按钮?

JsFiddle 的jsfiddle

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.

相关问题 Javascript-定位所有HTML选择的内容,而不是按ID或类别 - Javascript - target all html selects, not by ID or class 标签中没有ID名称或特定类时,如何使用JavaScript将文本添加到HTML? - How to add text to HTML using javascript when there is no id name or particular class is present inside a tag? 如何使用 javascript 和 HTML 将变量设置为 ID 和 Class 中的内容 - How to set a variable to something inside an ID and a Class with javascript and HTML 使用Javascript .getElement在类中定位目标P - Target P inside class using Javascript .getElement Javascript Function参数以html ID为目标并添加CSS类 - Javascript Function parameter to target html ID and add CSS Class 如何使用 JavaScript 创建 html 元素并为其创建类/ID - How to create html element using JavaScript and create class/id to it 使用Javascript,如何定位和更改没有ID或类的图像的URL? - Using Javascript, how can I target and alter the URL of an image with no ID or class? 如何定位没有ID或Class属性的iframe中的元素? - How to target an element inside an iframe that has no Id or Class attributes? 使用javascript定位类 - Using javascript to target class 如何使用javascript或jQuery在标签内使用已知的html内容查找标签的ID? - How do I look for a tag's ID using the known html content inside the tag using javascript or jQuery?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM