Operation of JS in iOS

Source: Internet
Author: User

Get all html:nsstring *ljs = [WebView stringbyevaluatingjavascriptfromstring:@ "Document.documentElement.innerHTML"];

Get body:

[WebView stringbyevaluatingjavascriptfromstring:@ "Document.documentElement.innerText"];

To remove a property:

[WebView stringbyevaluatingjavascriptfromstring:[nsstringstringwithformat:@] Document.images[%d].removeattribute ("width") ", I]];

Modify the address of a picture

[WebView stringbyevaluatingjavascriptfromstring:@ "document.images[0].src=" http://www.baidu.com/ W020110907540066716627.jpg ";d ocument.images[1].src=" http://www.baidu.com/W020120807265226549111.jpg ""];

[WebView stringbyevaluatingjavascriptfromstring:[nsstringstringwithformat:@ "document.images[%d].src="%@ "", I, ImageName]];

Tutorial:

Http://www.w3school.com.cn/htmldom/met_doc_getelementsbytagname.asp

Document Object Content Collection

Document Text block Object-javascript script language description
———————————————————————
Note: The names of the elements on the page, the Name property, and the JavaScript reference must consistently include case
Otherwise you will be prompted with an error message "The referenced element is empty or is not an object \\\\\"
———————————————————————

Object Properties
Document.title//Set document title equivalent to the title tag of HTML
Document.bgcolor//Set page background color
Document.fgcolor//Set foreground (text color)
Document.linkcolor//non-clicked link color
Document.alinkcolor//Activate link (focus on this link) color
Document.vlinkcolor//Clicked link color
Document. URL//Set URL property to open another page in the same window
Document.filecreateddate//File creation date, read-only property
Document.filemodifieddate//File modification date, read-only property
Document.filesize//File size, read-only property
Document.cookie//Set up and read out cookies
Document.charset//Set character sets Simplified Chinese: gb2312
———————————————————————
Common Object Methods
document.write ()//write content to page dynamically
DOCUMENT_CREATEELEMENT_X_X_X_X_X_X (tag)//Create an HTML tag object
document.getelementbyidx_x_x_x_x_x_x (ID)//Get the object with the specified ID value
Document.getelementsbyname (name)//Gets the object with the specified Name value
DOCUMENT.BODY.A (Otag)
———————————————————————

body-Principal Sub-object
Document.body//Specifies that the start and end of a document body is equivalent to body>/body>
Document.body.bgColor//Sets or gets the background color behind the object
Document.body.link//non-clicked link color
Document.body.alink//Activate link (focus on this link) color
Document.body.vlink//Clicked link color
Document.body.text//Text color
Document.body.innerText//Set text between body>.../body>
Document.body.innerHTML//Set HTML code between body>.../body>
Document.body.topMargin//page top margin
Document.body.leftMargin//Page left margin
Document.body.rightMargin//Page right margin
Document.body.bottomMargin//Page Bottom margin
Document.body.background//Background image

DOCUMENT.BODY.A (Otag)//dynamically generate an HTML object

Common Object Events
Document.body.onclick= "func ()"//mouse pointer Click object is triggered
Document.body.onmouseover= "func ()"//mouse pointer is triggered when moving to an object
Document.body.onmouseout= "func ()"///mouse pointer is triggered when the object is moved out
———————————————————————
location-Position Sub-object

document.location.hash//#号后的部分
Document.location.host//domain + port number
Document.location.hostname//Domain name
Document.location.href//Full URL
Document.location.pathname//directory section
Document.location.port//Port number
Document.location.protocol//Network Protocol (http:)
Section after Document.location.search//?

Documeny.location.reload ()//Refresh Web page
Document.location.reload (URL)//Open a new page
Document.location.assign (URL)//Open a new page
Document.location.replace (URL)//Open a new page
———————————————————————
selection-Selection Sub-object
Document.selection
———————————————————————

Images collection (images in a page)

A) by collection reference
Document.images//img tag on the corresponding page
Document.images.length//Number of IMG tags on the corresponding page
Document.images[0]//1th img Tag
Document.images[i]//i-1 img Tag

b) direct reference via Nane property
IMG name= "Oimage"
Document.images.oImage//document.images.name Properties

c) The SRC attribute of the reference picture
Document.images.oImage.src//document.images.name property. src

d) Create an image
var oimage
Oimage = new Image ()
Document.images.oimage.src= "1.jpg"
Also create an IMG/tag on the page that corresponds to the display

———————————————————————-

Forms collection (forms in a page)

A) by collection reference
Document.forms//The form label on the corresponding page
Document.forms.length//Number of/formform labels on the corresponding page
Document.forms[0]//1th/formform label
Document.forms[i]//i-1/formform label
Number of controls in Document.forms[i].length//I-1/formform
DOCUMENT.FORMS[I].ELEMENTS[J]//i-1/formform j-1 controls

b) direct reference by Tag Name property
/formform name= "Myform" >inputname= "Myctrl"/>/form
Document. Myform.myctrl//document. Table sole name. Control Name

c) Access the properties of the form
Document.forms[i].name//Corresponding Form Name> property
Document.forms[i].action//corresponding/formform action> property
document.forms[i].encoding//corresponding/formform enctype> property
Document.forms[i].target//corresponding/formform target> property

DOCUMENT.FORMS[I].A (Otag)//dynamically insert a control
DOCUMENT.ALL.ODIV//reference layer Odiv
document.all.odiv.style.display= "//layer is set to visual
Document.all.odiv.style.display= "None"//layer is set to hidden
Document.getelementid ("Odiv")//referencing objects by Getelementid
Document.getelementid ("Odiv"). style= "
Document.getelementid ("Odiv"). display= "None"

4 properties of a Layer object
Document.getelementbyidx_x_x_x_x_x_x ("ID"). InnerText//Dynamic output text
Document.getelementbyidx_x_x_x_x_x_x ("ID"). InnerHTML//Dynamic output HTML
Document.getelementbyidx_x_x_x_x_x_x ("ID"). Outertext//With innertext
Document.getelementbyidx_x_x_x_x_x_x ("ID"). outerHTML//With innerHTML

Document.getelementsbyname and document.getElementsByTagName

When the control on the page has the same name and multiple, from the strict point of view of the program, it is necessary to judge the length, and the length and the length are two kinds of reference methods.

oele= document.all.aaa;//There is an AAA object here, but we don't know how long it is now, so there is no way to manipulate it. Therefore, we have to do the process of judging the length first.

if (oele.length) {}else{};

In both cases, the contents of the curly braces are not the same:

if (oele.length) {

for (var i = 0; i<oele.length;i++) {

Oele[i].value.

}

}

else{

Oele.value.

};

But is it too complicated to write? And when the code in curly braces is more, we write two times code, dizzy first ~

Okay there

Document.getelementsbyname ()

This method. It is the same for one and more processing, we can use:

Oele = Document.getelementsbyname (' aaa ')

To reference

When Oele only 1, then is oele[0], there are many times, with the subscript method Oele[i] Cyclic acquisition, is not very simple?

It is worth mentioning that it is equally valid for name and ID. (It can only be applied to document objects)

<div Id=radiodiv language=javascript >

<input name=radio1 value=1type= "Radio" >

<input name=radio1 value=0type= "Radio" checked>

</div>

Document.getelementsbyname ("Radio1"). Item (0). Value result is 1

But. Correspondingly, there is another method that can be applied to a wider range of objects:

getElementsByTagName

<div Id=radiodiv language=javascript >

<input name=radio1 value=1type= "Radio" id=myradio1>

<input name=radio1 value=0type= "Radio" Id=myradio2 CHECKED >

</div>

Radiodiv.getelementsbytagname_r ("Input"). Item (0). Value result is 1

, like I know a <div id= ' aaa ' ><input name=input1value=1 type=radio ><input name=input1 value=2type=radio> ......</div> I'm going to take all of the input in the DIV, so I can write it:

Aaa.getelementsbytagname_r (' INPUT ')

This can be effective and other div (for example, there is a div called BBB, inside the same input) is different.

Correspond with getElementsByTagName,

There is also a document.body.all.tags ()

The object that can use this method is much smaller than getelementsbytagname. But more than Getelementsbyname.

We're going to have to mention getElementById here.

It is also only the document object can be used, and return is the first element of the array, hehe, its method name is getelement rather than getelements

<div Id=radiodiv language=javascript >

<input name=radio1 value=1type= "Radio" Id=myradio1 >

<input name=radio1 value=0type= "Radio" Id=myradio2 value= "MyVal2" CHECKED >

</div>

Document.getelementbyidx_x_x_x_x ("MyRadio2"). Value result is MyVal2

Operation of JS in iOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.