简体   繁体   English

按下独特的按钮即可执行操作

[英]Press a unique button do the actions

So as I started using JavaScript and jQuery, I have a question with unique div. 因此,当我开始使用JavaScript和jQuery时,我有一个关于唯一div的问题。

Is is possible in the JavaScript to make a unique div name then do the action onclick ? 是否可以在JavaScript中创建唯一的div名称,然后执行onclick操作?

http://jsfiddle.net/agmr2ytd/4/ http://jsfiddle.net/agmr2ytd/4/

Example HTML: HTML示例:

<div id="favoriteXBSbQG6fNlObroDG4ML2l9VRO/yNpvIFxL0Qjr6bP2A=">
    <a href="#" id="XBSbQG6fNlObroDG4ML2l9VRO/yNpvIFxL0Qjr6bP2A=" class="btn btn-danger">Me</a>
</div>
<div id="favoriteakkbN3eo8h0Q7S4ouHqMX7cU9vNLNKw3llO/PK0e9qI=">
    <a href="#" id="akkbN3eo8h0Q7S4ouHqMX7cU9vNLNKw3llO/PK0e9qI=" class="btn btn-danger">Me 2</a>
</div>

JavaScript: JavaScript:

$(function() {
    $('.favorite').click(function() {
        var element = $(this);
        var verify = element.attr("id");
        alert(verify);
        $('#favorite'+verify).hide();
    });

});

When I press a first a from div I wanna get the id value as a alert and hide it. 当我按div中的第a ,我想获取id值作为alert并将其隐藏。

You need to set class = favorite to your divs, then this is working: 您需要为您的div设置class =最喜欢的,然后就可以了:

DEMO 演示

jQuery / javascript: jQuery / JavaScript:

$(function() {
    $('.favorite').click(function() {
        var element = $(this);
        var verify = element.attr("id");
        alert(verify);
        var tohide = document.getElementById(verify);
        tohide.style.display = 'none';
    });

});

HTML: HTML:

<div id="favoriteXBSbQG6fNlObroDG4ML2l9VRO/yNpvIFxL0Qjr6bP2A=" class="favorite">
    <a href="#" id="XBSbQG6fNlObroDG4ML2l9VRO/yNpvIFxL0Qjr6bP2A=" class="btn btn-danger">Me</a>
</div>
<div id="favoriteakkbN3eo8h0Q7S4ouHqMX7cU9vNLNKw3llO/PK0e9qI="  class="favorite">
    <a href="#" id="akkbN3eo8h0Q7S4ouHqMX7cU9vNLNKw3llO/PK0e9qI=" class="btn btn-danger">Me 2</a>
</div>

Your selector is wrong, for attributes ^= means "attribute value starts with": 您的选择器错误,因为属性^=表示“属性值始于”:

$(function() {
    $('[id^=favorite] a').click(function() {
        var element = $(this);
        var verify = element.attr("id");
        alert(verify);
        $('#favorite'+verify.replace(/([ #;?%&,.+*~\':"!^$[\]()=>|\/@])/g,'\\$1')).hide();
    });
});

Example Fiddle <- 小提琴示例 <-

Your ID has to be escaped, used this answer for solution. 您的ID必须转义,请使用此答案作为解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 按同一个键做不同的动作 - Press the same key to do different actions 当我按下影响效果的按钮时,如何做向下滑动效果以不使我的网页回到顶部? - How can I do the slide down effect to not bring my web page back to top when I press the button that actions the effect? 其次通过按钮执行功能 - Second do function by press button 每当我在JavaScript中按下按钮时,如何生成唯一ID - How to generate a unique id everytime I press a button in JavaScript React - Native &#39; Redux Uncaught Error: Actions 必须是普通对象。 按下按钮时使用自定义中间件进行异步操作 - React - Native ' Redux Uncaught Error: Actions must be plain objects. Use custom middleware for async actions' on button press 处理-按下按钮时执行X键3秒钟-忽略其他按钮按下 - Processing - On button press do X for 3 sec - ignore further button presses 如何仅阻止页面上按键的特定默认操作? - How do I prevent only specific default actions of key press on a page? 如何获得不透明性以停止按下按钮的褪色? - How do I get the opacity to stop fading with a button press? 如何实现按住按钮 javascript? - How do I implement press and hold button javascript? 如何在按下按钮时创建一个框 - How do I create a box upon a button press
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM