jquery 获取父类窗口的某个标签
时间: 2023-11-18 12:12:15 浏览: 43
在 jQuery 中,可以使用 `parent()` 方法来获取父级元素,然后再使用 `find()` 方法来查找特定的标签。例如,如果要获取父级元素中的 `div` 标签,可以使用以下代码:
```javascript
// 获取父级元素
var parentElement = $("#childElement").parent();
// 查找特定的标签
var divElement = parentElement.find("div");
```
其中,`#childElement` 是子元素的 ID,`parent()` 方法返回父级元素,`find()` 方法在父级元素中查找特定的标签,并返回该标签的 jQuery 对象。如果父级元素中有多个相同的标签,可以使用 `eq()` 方法来获取其中一个标签,例如:
```javascript
// 获取第二个 div 标签
var secondDivElement = parentElement.find("div").eq(1);
```
其中,`eq(1)` 表示获取第二个标签,因为数组下标从 0 开始。
相关问题
子类弹窗获取父类窗口的属性 jquery'
在 jQuery 中,可以使用以下方法来获取父类窗口的属性:
1. 在子类弹窗中使用父类窗口的选择器来获取属性,例如:
```
class Parent {
constructor() {
this.attribute = "父类属性";
this.child_window = new Child();
}
}
class Child {
constructor() {
this.get_attribute();
}
get_attribute() {
let attribute = $(window.parent.document).find("#parent_window_id").data("attribute");
console.log(attribute);
}
}
```
在这个例子中,子类弹窗 Child 在初始化时调用 get_attribute 方法,该方法使用父类窗口的选择器 $(window.parent.document).find("#parent_window_id") 来获取父类窗口的某个元素,然后使用 jQuery 的 data 方法来获取该元素的 data 属性,从而获得父类窗口的属性值。
2. 在父类窗口中将属性值保存在 cookie 中,子类弹窗中读取 cookie 来获取属性值,例如:
```
class Parent {
constructor() {
this.attribute = "父类属性";
document.cookie = "attribute=" + this.attribute;
this.child_window = new Child();
}
}
class Child {
constructor() {
this.get_attribute();
}
get_attribute() {
let attribute = document.cookie.replace(/(?:(?:^|.*;\s*)attribute\s*\=\s*([^;]*).*$)|^.*$/, "$1");
console.log(attribute);
}
}
```
在这个例子中,父类窗口在初始化时把属性值保存在 cookie 中,子类弹窗在初始化时调用 get_attribute 方法,该方法使用 JavaScript 的 document.cookie 属性来获取 cookie,然后使用正则表达式来从 cookie 字符串中提取属性值,从而获得父类窗口的属性值。
怎样通过子类窗口获取父类窗口点击事件所获取的数据 jquery
在子窗口中获取父窗口点击事件获取的数据,可以通过在子窗口中监听 `window.opener` 的事件来实现。具体的实现方式取决于使用的框架和库。
以 jQuery 为例,可以在子窗口中通过 `window.opener` 访问到父窗口,并监听父窗口触发的事件。假设父窗口有一个按钮,点击按钮会触发一个 `click` 事件,并传递一些数据,可以在子窗口中通过以下方式获取到这些数据:
在父窗口中:
```html
<button id="my-button">Click me</button>
<script>
$('#my-button').click(function() {
var data = { foo: 'bar' }; // 假设要传递的数据是一个对象
window.open('child.html', '_blank', 'width=400,height=400');
window.localStorage.setItem('my-data', JSON.stringify(data));
});
</script>
```
在子窗口中:
```html
<script src="https://code.jquery.com/jquery-3.6.0.min.js"></script>
<script>
$(document).ready(function() {
var data = JSON.parse(window.localStorage.getItem('my-data'));
console.log(data); // 输出 { foo: 'bar' }
window.localStorage.removeItem('my-data');
$(window.opener.document).on('my-event', function(event, data) {
console.log(data); // 输出 { foo: 'bar' }
});
});
</script>
```
在子窗口中,获取到数据后,可以使用 `$(window.opener.document)` 来访问父窗口的 DOM,然后通过 `on` 方法来监听自定义事件,并在回调函数中处理父窗口传递过来的数据。在父窗口中,可以使用 `window.localStorage` 来存储数据,并在子窗口中读取。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)