Home  >  Article  >  Web Front-end  >  JavaScript review record - various uses of output

JavaScript review record - various uses of output

php是最好的语言
php是最好的语言Original
2018-08-02 09:59:00960browse

I have studied the output content in JS before, but now I turned it over and looked at it, and it is very unfamiliar. Therefore, in case of memorization, it is best to write it down to impress me most, and it will be much easier to review in the future.

1. Output content document.write

document.write() can be used to directly write content to the HTML output stream and output content directly to the web page.

(1) The output content is directly enclosed in " ".

document.write("Hello World!");

(2) Output content through variables.

var myVar="Hello World!";
document.write(myVar);

(3) Output multiple contents and connect them with numbers.

var myVar="Hello";
document.write(myVar + "World!");

(4) Output HTML tags, and the tags are enclosed in " ".

var myVar="Hello World!";
document.write(myVar + "</br>");

2. Output spaces

Because of the browser display mechanism, manually typed spaces will display multiple consecutive spaces into one space.

(1) Use the output HTMl tag   to solve the problem.

document.write("  "+"1"+"   "+"23");
//输出结果:  1   23

(2) Use CSS styles to solve it.

document.write("<span style=&#39;white-space:pre&#39;>"+"  1   23"+"</span>");
//输出结果:  1   23

3. Warning, alert message dialog box

No other operations can be performed before clicking the "OK" button in the dialog box. The message dialog box can usually be used to debug the program. alert output content, which can be a string or variable, is similar to document.write.

4. Confirmation, confirm message dialog box

The confirm message dialog box is often used to allow users to make choices. Syntax: confirm(str); where str is the text to be displayed in the message dialog box, and the return value is a Boolean value.

var myMessage=confirm("你想学习吗?");
if(myMessage==true){
    document.write("想的,好好学习。");
}
else{
    document.write("不,你想!");
}

note: The user cannot perform other operations before clicking the dialog button.

5. Question, prompt message dialog box

prompt pops up a message dialog box, usually used to ask for some messages that require interaction with the user. The prompt message dialog box includes an OK button, a Cancel button and a text input box. Syntax: prompt(str1,str2); where str1 is the text to be displayed in the message dialog box and cannot be modified, str2 is the content in the text box and can be modified.

var myName=prompt("请输入您的大名:");
if(myName!=Null){
    alert("Hello "+myName);
}
else{
    alert("Hello my friend.");
}

6. Open a new window. The window.open

open() method can find an existing or newly created browser window. Syntax: window.open([URL],[window name],[parameter string]); among them,

URL: optional parameter, the URL or path of the web page to be displayed in the window, if omitted or If the value of this parameter is an empty string, the window will not display the document.

Window name: The name consists of letters, numbers and _. Names with special meaning: _blank, display the target web page in a new window; _self, display the target web page in the current window; _top, display the target web page in the upper window of the frame web page. Different windows require different names, and there cannot be spaces in the name.

Parameter string:

Parameter Value illustrate
top Number Number of pixels from the top of the window to the top of the screen
left Number The number of pixels from the left end of the window to the left end of the screen
width Number Window width
height Number Window height
menubar yes,no Whether the window has a menu
toolbar yes,no Whether the window has a toolbar
scrollbars yes,no Whether the window has scroll bars
status yes,no Whether the window has a status bar
7. Close the window, window.close
var myWin=window.open("https://blog.csdn.net/bertZuo");    //将新打开的窗口对象存储在变量myWin中
myWin.close();    //关闭窗口

Related articles:
Summary of various methods of Javascript pop-up windows_javascript skills

Related Video:

JavaScript basic syntax and basic statement video tutorial

The above is the detailed content of JavaScript review record - various uses of output. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn