gpt4 book ai didi

javascript - document.querySelector() 返回 null

转载 作者:技术小花猫 更新时间:2023-10-29 12:32:21 28 4
gpt4 key购买 nike

我正在创建一个 polymer 元素。我已经制作了模板,现在正在编写脚本。出于某种原因,document.querySelector 为类和 id 选择器返回 null。不确定这是否不适用于 polymer (没有理由不应该)或者我没有导入任何东西或者还有什么问题。

事件卡片.html

<link rel="import" href="../components/polymer/polymer.html">
<link rel="import" href="event-card-description.html">

<polymer-element name="event-card" attributes="header image description">
<template>
<style>
.card-header {
display: block;
position: static;
font-size: 1.2rem;
font-weight: 300;
width: 300px;
height: 300px;
border-radius: 50%;
-webkit-border-radius: 50%;
-moz-border-radius: 50%;
overflow: hidden;
}
event-card-description {
margin: 0;
position: relative;
top: 225px;
}
</style>

<div
style="background: url(../images/{{image}}.jpg) no-repeat; background-size: cover"
class="card-header" layout horizontal center
on-mouseover='{{onHovered}}'
on-mouseout='{{onUnhovered}}'>
<event-card-description
id="description"
header={{header}}
description={{description}}>
</event-card-description>
</div>
</template>

<script>
Polymer('event-card', {
onHovered: function() {
var elem = document.querySelector("#description");
console.log(elem);
}
});
</script>
</polymer-element>

最佳答案

<event-card-description id="description">在您元素的影子 dom 中。 document.querySelector("#description")正在寻找 id#description 的节点在主文档中。预计找不到该节点,因为 shadow dom 边界隐藏了它。尝试:

this.shadowRoot.querySelector("#description");

但是,Polymer 有一个很棒的特性,其中具有 id 的静态元素被映射到 this.$.<id> .您可以使用 this.$.description获取该元素。

关于javascript - document.querySelector() 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25100883/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com