1

I have the following HTML:

<div data-name="countrySelectorRoot">
    <select>
        <option value="C1">Country 1</option>
        <option value="C2">Country 2</option>
        <option value="C3">Country 3</option>
        <option value="C4">Country 4</option>
        <option value="C5">Country 5</option>
    </select>

    <script type="text/javascript">
        // Some script which finds parent div...
    </script>
</div>

Inside script, I need code which needs to find my "countrySelectorRoot" div (var MyRoot = // Code that finds element with data-name="countrySelectorRoot").

However, I can have multiple divs with same data-name attribute and same contents, but I need to find the the specific one which is the parent of the script.

How to do this?

4

4 回答 4

1

尝试这个

   $('div[data-name="countrySelectorRoot"][/Script]')

它应选择作为脚本父级的特定脚本。

于 2012-06-16T14:09:30.530 回答
0

尝试eq()方法:

$('div[data-name="countrySelectorRoot"]:eq(0)') // this selects first div

:has

$('div[data-name="countrySelectorRoot"]:has(script)') 
于 2012-06-16T14:08:01.097 回答
0

这应该给你一个想法:

$('div script').parent()

jsBin 演示

但老实说,我认为您以错误的方式使用 javascript。

于 2012-06-16T14:14:53.057 回答
0

要专门针对其中包含脚本标记的那个,请使用:has过滤器

$('[data-name="countrySelectorRoot"]:has(script)');
于 2012-06-16T14:15:26.660 回答