当前位置: 动力学知识库 > 问答 > 编程问答 >

javascript - Show context-menus only when certain elements are clicked

问题描述:

I am kinda stuck with something and I need your help.

I am trying to show context-menus only when a user right-clicks on a certain elements in the page.

I thought I solve this problem by using getElementByClassName(...) and adding an onClick listener to each one of the elements, and when the user clicks on any of them I will then create the context-menus. And then remove the content menu later when everything is done.

Problem is that I don't have the full class names of those elements, all I know that they start with "story".

I am not sure how to go about doing this. Is there a way to use regex and getting all elements with a class name of story? Or is that not possible.

Thanks in advance,

网友答案:

There's this library that allows for regex selectors.

<div class="story-blabla"></div>

$("div:regex(class, story.*)")

However, you may not want to implement a full library. There's another solution:

$('div').filter(function() {
    return this.class.match(/story.*/);
    })

This will return the objects you want.

网友答案:

You can do this using attribute starts with selector

document.querySelectorAll("[class^=story]")

分享给朋友:
您可能感兴趣的文章:
随机阅读: