gpt4 book ai didi

Typescript 属性 'style' 在类型 'HTMLElement' 上不存在

转载 作者:搜寻专家 更新时间:2023-10-30 20:35:06 24 4
gpt4 key购买 nike

虽然我的代码有效(如下),但 typescript 会抛出错误,提示“类型‘HTMLElement’上不存在属性‘style’。”

如您所见,我正在通过 jQuery 和元素的 ID 获取元素。

$(this.id).click(function () {
this.style.display = 'none';
});

但这会导致以下错误:

`TS2339: Property 'style' does not exist on type 'HTMLElement'`.

我不确定处理此 ts 错误的正确方法是什么。

有人可以告诉我吗?

提前谢谢你。

更多代码,因为它被要求:

`/**
* Class represents a flash message
*/
export class FlashMessage {
flashMessageParentDivID:string;
iconID:string;
textID:string;
$: JQuery;

/**
*
* @param {string} flashMessageParentDiv - the string of the div's ID.
*/
constructor (flashMessageParentDiv: string, $: JQuery) {
this.flashMessageParentDivID = "#"+flashMessageParentDiv;
this.iconID = "#flash-message-icon",
this.textID = "#flash-message-text";
this.$ = $;
}

/**
* Displays the passed in message with the appropriate status.
* @param {string} message - the message we want to flash.
* @param {string} status: either 'error' or 'success'
*/
showWithMessage = function (message: string, status: string) {
//@todo: maybe throw an error if status does not equal error or success.
this.clearAndCreateFlashMessageInnerds();
this.$(this.flashMessageParentDivID).removeAttr("class");
this.$(this.flashMessageParentDivID).addClass(status);

this.$(this.iconID).removeAttr("class");
this.$(this.iconID).addClass("k-icon k-i-" + status);

this.$(this.textID).text(message);
this.$(this.flashMessageParentDivID).show();

this.$(this.flashMessageParentDivID).click(function () {
(<any>this).style.display = 'none';
});
};

...`

typescript throwing errors

最佳答案

在你的方法中使用它

(<HTMLElement>document.querySelector('#yourDomElmentID')).style.display = 'none';

test code

我测试了你的代码,它在我的代码中编译成功。

关于Typescript 属性 'style' 在类型 'HTMLElement' 上不存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47997480/

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