The .innerHTML property sets or returns the HTML content inside the selected element (i.e. between the tags).
document.querySelector('.card').innerHTML
There's also the rarely used .outerHTML property. .outerHTML represents the HTML element itself, as well as its children.
<h1id="pick-me">Greetings To <span>All</span>!</h1>constinnerResults=document.querySelector('#pick-me').innerHTML;console.log(innerResults);// logs the string: "Greetings To <span>All</span>!"constouterResults=document.querySelector('#pick-me').outerHTML;console.log(outerResults);// logs the string: "<h1 id="pick-me">Greetings To <span>All</span>!</h1>"
.innerText will get the visible text of the element. If CSS is used to hide any text inside that element, .innerTextwill not return that text, while .textContentwill return it.
This is stated above, but I wanted to call this out, specifically. When you're using the .appendChild() method, it must be called on an existing element. To be clear, you can't call this on the document object, so the following will result in an error:
Here's the .removeChild() documentation page on MDN: removeChild docs
可以看出來first child其實有奇怪的內容
到element點選右鍵可以用Edit as HTML去看其底下真正的HTML。
2. Removing a Child Element (Part 2!)
A drawback (and workaround!) with the .removeChild() Method
Just like the .appendChild() method, there's a (somewhat minor) drawback to the .removeChild() method. Both methods:
require access to parent to function
可以利用parentElement方法來做到這件事:
不需要parent的remove方式:
Remove Page Content Recap
In this short section, we learned two ways to remove an element from the page. You learned about:
.removeChild()
.remove()
The difference is that with .removeChild() must be called on the parent of the element being removed and must be passed the child to be removed, while .remove() can be called directly on the element to delete.
We also learned about the following helpful properties:
.firstChild
.firstElementChild
.parentElement
The difference between .firstChild and .firstElementChild, is that .firstElementChild will always return the first element, while .firstChildmight return whitespace (if there is any) to preserve the formatting of the underlying HTML source code.
Style Page Content
CSS Specificity
廣度概念:
Rules in a stylesheet > Rules in a <style tag> > Rules in a tag's style attribute
"specificity" is: the means by which browsers decide which CSS property values are the most relevant to an element and, therefore, will be applied.
Basically, the closer the style rule is to an element, the more specific it is. For example, a rule in a style attribute on an element will override a style rule for that element in a CSS stylesheet. There is also the specificity of the type of selector being used. An _ID_ is more specific than a class.
這個概念非常重要,有很多地方可以修改style,所以必須知道誰可以override誰。
1. Modifying an Element's Style Attribute
修改element其中一個style:
When trying to style an element, the most-specific rules that you can write for an element are written in that element's style attribute. Lucky for us, we can access an element's style attribute using the .style property!
Check out the documentation page for more information: style docs
顯示所有可以指定的field
2. Adding Multiple Styles At Once
Fortunately, we can use the .style.cssText property to set multiple CSS styles at once!
Notice that when using the .style.cssText property, you write the CSS styles just as you would in a stylesheet; so you write font-size rather than fontSize. This is different than using the individual .style.<property> way.
3. Setting An Element's Attributes
Another way to set styles for an element is to bypass the .style.<property> and .style.cssText properties altogether and use the .setAttribute() method:
為了遵守Separation of concern,javascript最好不要管理style,那是css的工作。那javascript要幹嘛?
利用javascript找element,然後賦予已經在css定義好的class。
1. The .className Property
如何顯示一個element所有的class?
We could use .className to access the list of classes:
The .className property returns a space-separated string of the classes. This isn't the most ideal format, unfortunately. We can, however, convert this space-separated string into an array using the JavaScript string method, .split():
有了class list之後,就可以做以下操作:
use a for loop to loop through the list of class names
use .push() to add an item to the list
use .pop() to remove an item from the list
設定class:
The above code erases any classes that were originally in the element's class attribute and replaces it with the single class im-the-new-class.
2. The .classList Property
Since .className returns a string, it makes it hard to add or remove individual classes. As I mentioned earlier, we can convert the string to an array and then use different Array Methods to search for a class remove it from the list, and then update the .className with the remaining classes. However, we don't want to do all of that work! Let's use the newer .classList property.
The .classList property has a number of properties of its own. Some of the most popularly used ones are:
.add() - to add a class to the list
.remove() - to remove a class from the list
.toggle() - to add the class if it doesn't exists or remove it from the list if it does already exist
.contains() - returns a boolean based on if the class exists in the list or not
最後說明一件事:為什麼 import js file都要擺在最後面?
可能因為html還沒有paint,dom還沒形成,所以js找不到element,無法做修改。
Style Page Content Recap
We learned a ton of content in this section! We looked at:
modifying individual styles with .style.<prop>
updating multiple styles at once with .style.cssText
getting/setting a list of classes with .className
getting/setting/toggling CSS classes with .classList
My recommendation to you is that, out of the list of techniques you learned in this section, to use the .classList property more than any other. .classList is by far the most helpful property of the bunch, and it helps to keep your CSS styling out of your JavaScript code.
nanodegreeCard.textContent = "I will be the updated text for the nanodegreeCard element!";
// creates and returns a <span> element
document.createElement('span');
// creates and returns an <h3> element
document.createElement('h3');
// create a brand new <span> element
const newSpan = document.createElement('span');
// select the first (main) heading of the page
const mainHeading = document.querySelector('h1');
// add the <span> element as the last child element of the main heading
mainHeading.appendChild(newSpan);
const newSpan = document.createElement('span');
// causes an error
document.appendChild(newSpan);
const myPara = document.createElement('p');
const textOfParagraph = document.createTextNode('I am the text for the paragraph!');
myPara.appendChild(textOfParagraph);
document.body.appendChild(myPara);
const myPara = document.createElement('p');
myPara.textContent = 'I am the text for the paragraph!';
document.body.appendChild(myPara);
const mainHeading = document.querySelector('h1');
// add an ID to the heading's sibling element
mainHeading.nextElementSibling.setAttribute('id', 'heading-sibling');
// use the newly added ID to access that element
document.querySelector('#heading-sibling').style.backgroundColor = 'red';
<h1 id="main-heading" class="ank-student jpk-modal">Learn Web Development at Udacity</h1>
const mainHeading = document.querySelector('#main-heading');
// store the list of classes in a variable
const listOfClasses = mainHeading.className;
// logs out the string "ank-student jpk-modal"
console.log(listOfClasses);
const arrayOfClasses = listOfClasses.split(' ');
// logs out the array of strings ["ank-student", "jpk-modal"]
console.log(arrayOfClasses);
mainHeading.className = "im-the-new-class";
<h1 id="main-heading" class="ank-student jpk-modal">Learn Web Development at Udacity</h1>
const mainHeading = document.querySelector('#main-heading');
// store the list of classes in a variable
const listOfClasses = mainHeading.classList;
// logs out ["ank-student", "jpk-modal"]
console.log(listOfClasses);