开发者

Select div from string with jquery

I have string that contains some di开发者_高级运维v's how to get one of them by id ?

EDIT

I'm using right click selection plugin, so when i select a div .

 $("#first #second").contextMenu({
        menu: 'myMenu'
    },
        function(action, el, pos) {
      switch (action) {
            case "do":
                {

and the selection is basicley

$(el).html()

So this is the code that contains the html that is in #second, but the #second isn't one div , it is a'lot of divs with that id, and i want when i click on that div to select certain id and get it's context . Is there a easyer way ?


You can have jquery interpret a string as a set of tags like this:

$(htmlstring);

So something like this might work:

$("<div><div id='test'></div></div>").find("#test");

Edit: Now with the updated question this is of course not what you want.

You can't have more than one element with the same id. IDs are supposed to be unique identifiers.


In your case you can wrap el and traverse from it, like this:

$(el).find('.something-else').blah(); // ...

el is not a string, it's a DOM element.

Some of your confusion might stem from writing $(el).html(). If you were debugging with that, it'd look like a string because you were extracting the actual HTML held within the DOM element you had in hand.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜