0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[js]この要素は存在するか?変数まで!関数を作ったよ!

Posted at

こんにちは。名無しのnです!今日はこの要素は存在するか確かめるプログラムを書きます!
解説ももちろんします!

コード

main.js
function check_get(name,eleclass) {
   let getele ;//変数定義
    if (name[1]=="#"||name[1]==".") {//要素か
        if (name[1]==".") {//クラスなら
            
        if (eleclass) {//存在するか(Eleclass)
            getele=document.querySelectorAll(name)[eleclass]///eleclassはx番目だからget
        }else{
            getele=document.querySelector(name) //classget!
        }
        }
        
        if (getele==undefined) {//idならです。
            getele=document.querySelector(name)//getで代入
        }
        if (!getele) {//geteleではない
            return false //要素が存在しない
        }
        return getele
    }else{
        if (! name) {
            return false
        }
        return name
    }
}

まずは,関数を定義します。名前はcheck_getにしました。
let getele ;//変数定義で変数定義し,ほかもろもろは書いてありますw
ちなみに別名Tomakareですw
https://nanashinon-sample.glitch.me/scripts/check_get/index.js
コード

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?