0

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,

4

2 に答える 2

0

セレクターで始まる属性を使用してこれを行うことができます

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

于 2013-03-07T21:08:35.137 に答える
0

正規表現セレクターを可能にするこのライブラリーがあります。

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

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

ただし、完全なライブラリを実装したくない場合があります。別の解決策があります:

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

これにより、必要なオブジェクトが返されます。

于 2013-03-07T20:25:22.123 に答える