3

HTML Element classList Property in Go

 2 years ago
source link: http://siongui.github.io/2017/12/20/html-element-classlist-property-in-go/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Manipulate CSS classes of an HTML element via classList property of the element. We can add, remove, or toggle the class of an element, or test if some class exists in the class attribute of the element via contains method. For the full list of methods provided in classList, see Element.classList - Web APIs | MDN.

Manipulate classList Property

Assume we have the following div element in our HTML:

<div id="foo">Hello World</div>

And also the following CSS class:

.invisible { display: none; }

We will show you how to add the invisible class to the div element via the classList property. For other manipulations like remove, toggle, or contains, the usage is similar and we will not cover here.

JavaScript

var f = document.querySelector("#foo");

f.classList.add("invisible");

GopherJS

The above code in Go/GopherJS is as follows:

import (
      "github.com/gopherjs/gopherjs/js"
)

func main() {
      f := js.Global.Get("document").Call("querySelector", "#foo")

      f.Get("classList").Call("add", "invisible")
}

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK