Attribute

text()

// Getter
text(): string;
// Setter
text(string: string): QueryResult;

获取或者设置元素的 innerText

example

// Getter
console.log(nashi("p").text()) // nashi is small

//Setter
nashi("p").text("nashi")
console.log(nashi("p").text()) // nashi
<!-- Before -->
<p>nashi is small</p>

<!-- After -->
<p>nashi</p>

class()

// Getter
class(): string;
// Setter
class(className: string): QueryResult;

获取或者设置元素的 className

example

// Getter
console.log(nashi("p").class()) // content
console.log(document.querySelector("p").className); // content

// Setter
nashi("p").class("nashi is small")
<!-- Before -->
<p class="content">nashi is small</p>

<!-- After -->
<p class="nashi is small">nashi is small</p>

id()

// Getter
id(): string;
// Setter
id(id: string): QueryResult;

获取或者设置元素的 id

example

// Getter
console.log(nashi("p").id()) // content
console.log(document.querySelector("p").id); // content

// Setter
nashi("p").id("nashi is small")
<!-- Before -->
<p id="content">nashi is small</p>

<!-- After -->
<p id="nashi is small">nashi is small</p>

addClass()

// Setter
addClass(className: string): QueryResult;

对应元素添加 className 。

example

// Getter
nashi("p").addClass("nashi")
<!-- Before -->
<p>nashi is small</p>

<!-- After -->
<p class="nashi">nashi is small</p>

removeClass()

// Setter
removeClass(className: string): QueryResult;

移除对应元素的 className 。

example

// Getter
nashi("p").removeClass("small")
<!-- Before -->
<p class="nashi small">nashi is small</p>

<!-- After -->
<p class="nashi">nashi is small</p>

hasClass()

// Getter
hasClass(className: string): boolean;

返回元素是否拥有指定的 className 。

example

<p class="nashi is small">nashi is small</p>
console.log(nashi("p").hasClass("small")); // true
console.log(nashi("p").hasClass("nashi")) // false

toggleClass()

// Setter
toggleClass(className: string): QueryResult;

如果元素有这个 className,则移除它,否则添加它 。

example

<p class="nashi small">nashi is small</p>
const para = nashi("p")
para.toggleClass("small")
console.log(para.class()); // nashi

para.toggleClass("small")
console.log(para.class()); // nashi small

attr()

// Getter
attr(key: string): string;
// Setter
attr(key: string, value: string): QueryResult;

获取或者设置元素的 attribute

example

const para = nashi("p")
console.log(para.attr("class")); // nashi

para.attr("class", "nashi small")
console.log(para.class()); // nashi small
<!--Before-->
<p class="nashi">nashi is small</p>

<!--After-->
<p class="nashi small">nashi is small</p>

prop()

// Getter
prop(key: string): string;
// Setter
prop(key: string, value: string): QueryResult;

获取或者设置元素的 prop

html()

html(): string;
html(html: string): QueryResult;

获取或者设置元素的 innerHTML

example

const dv = nashi("div")
dv.html("<p>nashi is small</p>")
console.log(dv.html());
<!--Before-->
<div></div>

<!--After-->
<div>
  <p>nashi is small</p>
</div>

accesskey()

// Getter
accesskey(): string;
// Setter
accesskey(key: string): QueryResult;

获取或者设置元素的 accesskey

removeAttr()

// Setter
removeAttr(key: string): QueryResult

删除元素的 attribute

example

const para = nashi("p")
para.removeAttr("class")
console.log(para.class()); // ""
<!--Before-->
<p class="nashi">small</p>

<!--After-->
<p class="">small</p>
Last Updated:
Contributors: renovate[bot]