简体   繁体   English

使用jquery从字符串中选择div

[英]Select div from string with jquery

I have string that contains some div's how to get one of them by id ? 我有一些字符串包含一些div如何通过id获取其中一个?

EDIT 编辑

I'm using right click selection plugin, so when i select a div . 我正在使用右键选择插件,所以当我选择一个div。

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

and the selection is basicley 并且选择是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 . 所以这是包含#second中的html的代码,但是#second不是一个div,它是具有该id的div的'时刻,我想当我点击该div来选择某个id和得到它的背景。 Is there a easyer way ? 有更简单的方法吗?

You can have jquery interpret a string as a set of tags like this: 您可以让jquery将字符串解释为一组标记,如下所示:

$(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. 您不能拥有多个具有相同ID的元素。 IDs are supposed to be unique identifiers. ID应该是唯一标识符。

In your case you can wrap el and traverse from it, like this: 在你的情况下,你可以包装el和遍历它,如下所示:

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

el is not a string, it's a DOM element. el不是字符串,它是DOM元素。

Some of your confusion might stem from writing $(el).html() . 你的一些困惑可能源于写$(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. 如果您正在调试它,它看起来像一个字符串,因为您正在提取您手头的DOM元素中保存的实际HTML。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM