LoginSignup

Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

0
0

ใ€JavaScriptใ€‘querySelectorใซ่ค‡ๆ•ฐใฎๅฑžๆ€งใ‚’ๆŒ‡ๅฎšใ™ใ‚‹ใจใ€ใƒใ‚นใƒˆใ—ใŸ่ฆ็ด ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใ‚‹

Posted at

ใฏใ˜ใ‚ใซ

ไปฅไธ‹ใฎใ‚ˆใ†ใชHTML่ฆ็ด ใŒใ‚ใฃใŸใจใใซใ€ใฉใ†ใ™ใ‚Œใฐๅ†…้ƒจ่ฆ็ด ใ‚’ใƒ”ใƒณใƒใ‚คใƒณใƒˆใงๅ–ๅพ—ใงใใ‚‹ใฎใ‹ใจๆ€ใฃใฆใ„ใพใ—ใŸใ€‚

index.html
<div id="x">
    <div class="y">This is the element we are selecting</div>
</div>

่งฃๆฑบ็ญ–

querySelectorใงไปฅไธ‹ใฎใ‚ˆใ†ใซ่ฆช่ฆ็ด ใ‹ใ‚‰้ †ใซๆŒ‡ๅฎšใ—ใพใ™ใ€‚
ใใ‚Œใžใ‚Œใฎ่ฆ็ด ใฎ้–“ใซใ‚นใƒšใƒผใ‚นใ‚’ๆ›ธใใ“ใจใงใ€ใƒใ‚นใƒˆใ•ใ‚ŒใŸ่ฆ็ด ใซใ‚ขใ‚ฏใ‚ปใ‚นใ™ใ‚‹ใ“ใจใŒใงใใพใ—ใŸใ€‚

script.js
const a1 = document.querySelector("#x .y");
console.log(a1.innerText);
ใ‚ณใƒณใ‚ฝใƒผใƒซ
This is the element we are selecting

ใกใชใฟใซใ€่ฆช่ฆ็ด ใ‚’ๆ›ธใ‹ใชใใฆใ‚‚ใƒใ‚นใƒˆใ•ใ‚ŒใŸ่ฆ็ด ใซใ‚ขใ‚ฏใ‚ปใ‚นใงใใพใ™ใŒใ€ๆฌกใฎใ‚ˆใ†ใซ่ค‡ๆ•ฐใฎ่ฆ็ด ใŒใ‚ใ‚‹ใจใ€ๆœ€ๅˆใฎ่ฆ็ด ใซใ‚ขใ‚ฏใ‚ปใ‚นใ•ใ‚Œใพใ™ใ€‚

index.html
<div class="y">Dummy element</div>
<div id="x">
    <div class="y">This is the element we are selecting</div>
</div>
script.js
const a1 = document.querySelector("#x .y");
const a2 = document.querySelector(".y");

console.log(a1.innerText);
console.log(a2.innerText);
ใ‚ณใƒณใ‚ฝใƒผใƒซ
This is the element we are selecting
Dummy element
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
MhalTeddy

@MhalTeddy

้ƒฝๅ†…ใงใƒ‡ใƒผใ‚ฟใ‚ตใ‚คใ‚จใƒณใƒ†ใ‚ฃใ‚นใƒˆ/AIใ‚จใƒณใ‚ธใƒ‹ใ‚ข/ใƒžใƒใƒผใ‚ธใƒฃใƒผใจใ—ใฆๅƒใ„ใฆใ„ใพใ™ใ€‚2021ๅนดใซๆฉŸๆขฐใƒกใƒผใ‚ซใƒผใฎๆŠ€่ก“่€…ใ‹ใ‚‰่ปข่บซใ—ใพใ—ใŸใ€‚ๆœ€้ฉๅŒ–ใ€ๅผทๅŒ–ๅญฆ็ฟ’ใ‚’ไป•ไบ‹ใจใ—ใฆใŠใ‚Šใ€ๆœ€่ฟ‘ใฏLLMใƒ—ใƒญใ‚ธใ‚งใ‚ฏใƒˆใ‚‚ๅง‹ใ‚ใฆใ„ใพใ™ใ€‚็ตฑ่จˆๆคœๅฎš1็ดšใ€Kaggle Masterใ€‚

JavaScript's like ranking last week

ใŠ้กŒใฏไธๅ•๏ผQiita Engineer Festa 2024ใง่จ˜ไบ‹ๆŠ•็จฟ๏ผ
degudegu2510

ใ€HTMLใ€‘ใƒœใ‚ฟใƒณ่ฆ็ด ใซdisabledๅฑžๆ€งใ‚’ใคใ‘ใ‚‹ใฎใ‚’ใ‚„ใ‚ใพใ›ใ‚“ใ‹๏ผŸ

ใŠ้กŒใฏไธๅ•๏ผQiita Engineer Festa 2024ใง่จ˜ไบ‹ๆŠ•็จฟ๏ผ
saba_uni_toro_

ใ€ๅ€‹ไบบ้–‹็™บใ€‘่‰ฒๅฝฉๆคœๅฎš1็ดšๅˆๆ ผใฎใƒ‡ใ‚ถใ‚คใƒŠใƒผใŒๆœฌๆฐ—ใงใ€Œ่‰ฒๅฝฉๆคœๅฎšใฎ่‰ฒๅใ‚ขใƒ—ใƒชใ€ใ‚’ไฝœใฃใŸ่ฉฑ

Comments

ibyrjbf
@ibyrjbf

CSSใ‚ปใƒฌใ‚ฏใ‚ฟใƒผใง็–‘ไผผใ‚ฏใƒฉใ‚นใชใฉใ‚’ไฝฟใฃใŸๅฏพ่ฑกๅ–ๅพ—ใชใฉใ‚‚ใงใใ‚‹่‡ช็”ฑๅบฆใฎ้ซ˜ใ•ใŒgetElement(s)By๏ฝž็ณปใฎใƒกใ‚ฝใƒƒใƒ‰ใซใฏ็„กใ„querySelectorใ€querySelectorAllใฎใƒกใƒชใƒƒใƒˆใงใ™ใญใ€‚

sample.html
<table>
  <tr>
    <td>A</td><td>B</td><td>C</td>
  </tr>
  <tr>
    <td>D</td><td>E</td><td>F</td>
  </tr>
  <tr>
    <td>G</td><td>H</td><td>I</td>
  </tr>
</table>

<script>
const getTagleCellText = (x, y) =>
  document.querySelector(`table tr:nth-child(${y}) td:nth-child(${x})`)?.
    textContent ?? 'out of area';

console.log(getTagleCellText(1, 1));  // A
console.log(getTagleCellText(2, 3));  // H
console.log(getTagleCellText(-1, 0)); // out of area
</script>
0

Let's comment your feelings that are more than good

Being held Article posting campaign

ใใ†ใ ๏ผTestRailใ‚’ไฝฟใฃใฆใฟใ‚ˆใ†๏ผ๏ผ

~
View details

CodeAGIใงๅฎŸ้š›ใซใ‚ณใƒผใƒ‰ใ‚’่‡ชๅ‹•็”Ÿๆˆใ—ใฆใƒฌใƒ“ใƒฅใƒผใ‚’ๆŠ•็จฟใ—ใ‚ˆใ†๏ผ

~
View details
0
0

Login to continue?

Login or Sign up with social account

Login or Sign up with your email address