[英]jQuery: Selecting the second element when selectors are same using :eq
I have the following code. 我有以下代码。 There are two svg elements with same selectors , and i'm trying to select all the elements. 有两个具有相同选择器的svg元素,我正在尝试选择所有元素。
When i use selector [inner-text='" + innerText + "'][depth='" + depth + "']
I will be able to access the first element. 当我使用选择器[inner-text='" + innerText + "'][depth='" + depth + "']
我将可以访问第一个元素。 When i try to access the second element using :eq(2)
it gives an error Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '[inner-text='Exchanges data with'][depth='3']:eq(2)' is not a valid selector.
当我尝试使用:eq(2)
访问第二个元素时,出现错误Uncaught SyntaxError: Failed to execute 'querySelectorAll' on 'Document': '[inner-text='Exchanges data with'][depth='3']:eq(2)' is not a valid selector.
How can i access all the elements ( say loop over all the elements in case the selectors are same ) 我如何访问所有元素(例如,在选择器相同的情况下遍历所有元素)
$( document ).ready(function() { console.log( "ready!" ); var innerText = "Exchanges data with"; var depth = "3"; $( "#btn" ).click(function() { point1 = jQuery(d3.selectAll("[inner-text='" + innerText + "'][depth='" + depth + "']")[0]).attr('transform').toString(); alert(point1); point2 = jQuery(d3.selectAll("[inner-text='" + innerText + "'][depth='" + depth + "']:eq(2)")[0]).attr('transform').toString(); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg> <g class="node" transform="translate(10,10)" inner-text="Exchanges data with" nodeid="4-3" parent-list="test asset 1" child-list="test asset 9-" parent-id="6-2" child-id="-3" has-child="true" depth="3"> <circle r="4.5"></circle> </g> <g class="node" transform="translate(20,20)" inner-text="Exchanges data with" nodeid="2-3" parent-list="test asset 6" child-list="" parent-id="1-2" child-id="" has-child="true" depth="3"> <circle r="4.5" style="stroke: rgb(141, 214, 223); fill: rgb(141, 214, 223);"></circle> </g> </svg> <button id="btn">Click Me </button>
You got the error because you tried to use a jQuery extension ( :eq
) in a d4.selectAll
call. 因为在d4.selectAll
调用中尝试使用jQuery扩展名( :eq
),所以出现了d4.selectAll
。 Since :eq
is provided by jQuery, that won't work. 由于:eq
是由jQuery提供的,因此将无法使用。
selectAll
returns all matching elements, so selectAll
返回所有匹配的元素,因此
var points = d3.selectAll("[inner-text='" + innerText + "'][depth='" + depth + "']");
var point1 = jQuery(points[0][0]);
var point2 = jQuery(points[0][1]);
var transform1 = point1.attr('transform'); // No need for toString, it will always be a string
var transform2 = point2.attr('transform');
You can find out how many points there are from points.length
; 您可以从points.length
找出多少个点; individual points are accessible with indexes 0
through points.length - 1
. 单个点可通过索引0
到points.length - 1
进行访问。
You can use :nth-child
instead of :eq
, in this case. 在这种情况下,可以使用:nth-child
而不是:eq
。
Try below code. 尝试下面的代码。
$(document).ready(function() { console.log("ready!"); var innerText = "Exchanges data with"; var depth = "3"; $("#btn").click(function() { point1 = jQuery(d3.selectAll("[inner-text='" + innerText + "'][depth='" + depth + "']")[0]).attr('transform').toString(); alert(point1); point2 = jQuery(d3.selectAll("[inner-text='" + innerText + "'][depth='" + depth + "']:nth-child(2)")[0]).attr('transform').toString(); alert(point2); }); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.4.11/d3.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <svg> <g class="node" transform="translate(10,10)" inner-text="Exchanges data with" nodeid="4-3" parent-list="test asset 1" child-list="test asset 9-" parent-id="6-2" child-id="-3" has-child="true" depth="3"> <circle r="4.5"></circle> </g> <g class="node" transform="translate(20,20)" inner-text="Exchanges data with" nodeid="2-3" parent-list="test asset 6" child-list="" parent-id="1-2" child-id="" has-child="true" depth="3"> <circle r="4.5" style="stroke: rgb(141, 214, 223); fill: rgb(141, 214, 223);"></circle> </g> </svg> <button id="btn">Click Me</button>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.