这个JavaScript系列实际上做了什么?

时间:2017-03-23 21:45:51

标签: javascript

伙计们,我是Javascript的新手,我通过研究了解了这段代码中的所有内容,但是,我并不了解[9]和[12]行对代码的实际作用。我的猜测是它叫id" msg"当按下按钮但是那样做了什么?感谢所有帮助。

<!DOCTYPE html>
<html>
<body>


<p>Click the button to sort the array.</p>

<button onclick="RearrangeArray()">Rearrange Array</button>

<p id="msg"></p>



<script>

var products = ["Printer", "Tablet", "Router", "Computer","TV"];
document.getElementById("msg").innerHTML = products;

function RearrangeArray() {
    products.sort();
    document.getElementById("msg").innerHTML = products;
}

</script>

</body>
</html>

4 个答案:

答案 0 :(得分:2)

找到$file = Get-Content C:\test1.txt -Raw $result = $file | Select-String -Pattern '(?smi)(=============start============\s+string I want to find.*?=============end============)' $result.Matches.Value | Out-File -FilePath C:\processing.txt $file -replace '(?smi)(=============start============\s+string I want to find.*?=============end============)' | Out-File -FilePath C:\configfinal.txt 及其内部的任何内容,删除并用变量<element id="msg"></element>中的任何内容替换它,并解析我们在其中找到的任何HTML。

答案 1 :(得分:0)

找到ID为&#34; msg&#34;的元素并将它的值设置为产品,这是对数组的引用。

基本上,文件意味着this

getElementById是一个用HTML定位元素的函数。

您可以将innerHTML理解为元素的内容/值。

答案 2 :(得分:0)

<p>Click the button to sort the array.</p>

// create a new button
// when a user clicks this button, call the RearrangeArray function
<button onclick="RearrangeArray()">Rearrange Array</button>

// create a new paragraph element and set its id to "msg"
// by doing so, you can get a reference to this element and modify the contents
// or attributes using javascript
<p id="msg"></p>


<script>

// define an array containing the following strings
var products = ["Printer", "Tablet", "Router", "Computer","TV"];

// get the paragraph element defined earlier, then set its innerHTML
// property to the products

// because products is not a string, it is coerced to a string by
// joining each of the elements with a comma
document.getElementById("msg").innerHTML = products;


// defines a function that is available globally
// the button above refers to this function in the 'onclick' handler
function RearrangeArray() {

    // sorts the array of products above using the javascript array sort function
    // the products should now look like this: ["Computer", "Printer", "Router", "TV", "Tablet"]
    products.sort();

    // get the element with the id of "msg" (which you defined above)
    // and set its html to the now-sorted array of products (coerced as a string)
    document.getElementById("msg").innerHTML = products;
}

</script>

答案 3 :(得分:0)

所有HTML标记都可以具有id属性,并且在网页中它应该是唯一的,这是使用the document.getElementById("ID_value")将HTML标记的DOM对象保存在变量中的一种方法。此外,DOM对象的innerHTML属性将HTML存储在此Object的正确标记内。

所以,这一行:

document.getElementById("msg").innerHTML = products;

表示products数组的值作为纯HTML存储在具有msg ID的标记内。