簡體   English   中英

有沒有辦法在不知道其類名或ID的情況下選擇父div?

[英]Is there a way to select parent div without knowing its class name or ID ?

我有一個我在javascript庫d3中制作的圖表。 它的大小取決於窗口的大小,如下所示:

var GUIWidth = window.innerWidth;
var GUIHeight = window.innerHeight;

這工作正常,但如果我想把這個圖表放到另一個頁面,但讓它變小? 我不想進入這里並調整它,我希望能夠獲得其所容納的容器的大小並使用該寬度和高度。

但是,在我的情況下,我不知道容器的ID(因為另一個人正在開發UI)。

如何獲取保存圖表的父div的ID(以及最好的寬度和高度)。

這是示例代碼:

var svg = d3.select("#interface") //this is my graphs container
    .append("div")
    .attr("id", "svgContainer")
    .attr("width", GUIWidth)
    .attr("height", GUIHeight);

這里的interface是我的圖形容器,但我想做這樣的事情來獲得寬度和高度:

var GUIWidth = $(parentOf('#interface')).innerWidth();
var GUIHeight = $(parentOf('#interface')).innerHeight();

這可能嗎 ?

另外我想堅持使用javascript / jquery來執行此操作,因為我在我的代碼中的其他位置使用GUIWidthGUIHeight變量來確定其他div的大小:)

謝謝

試過這個?

var GUIWidth = $('#interface').parent().innerWidth();
var GUIHeight = $('#interface').parent().innerHeight();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM