2014年8月24日 星期日

【 js 筆記】持續更新,雜亂無章的筆記- 01

➜   Change Text in <p>
document.getElementById("helloText").innerHTML='Hello ';

➜   onclick function

In HTML:
<element onclick="myScript">
In JavaScript:
object.onclick=function(){myScript};
In JavaScript, using the addEventListener() method:
object.addEventListener("click", myScript);
Note: 
The addEventListener() method is not supported in Internet Explorer 8 and earlier versions. 
<script>
function myFunction(e){
  var x=e.currentTarget;
  alert("The id of the triggered element: " + x.id);
}
</script>
</head>

<body>
<p id="p1" onclick="myFunction(event)">
  Click on this paragraph. An alert box will
  show which element triggered the event.</p>
 Change element class
http://stackoverflow.com/questions/195951/change-an-elements-css-class-with-javascript

To change all classes for an element:

To replace all existing classes with one or more new classes, set the className attribute:
document.getElementById("MyElement").className = "MyClass";
(You can use a space-delimited list to apply multiple classes.)

To add an additional class to an element:

To add a class to an element, without removing/affecting existing values, append a space and the new classname, like so:
document.getElementById("MyElement").className += " MyClass";

To remove a class from an element:

To remove a single class to an element, without affecting other potential classes, a simple regex replace is required:
document.getElementById("MyElement").className =
   document.getElementById("MyElement").className.replace
      ( /(?:^|\s)MyClass(?!\S)/g , '' )
/* code wrapped for readability - above is all one statement */
➜   .getElementsByClassName
奇怪的沒辦法使用 
var A = document.getElementsByClassName("actived")[0]; 一定要分開使用 A[1]才可以,敲詭異的~
➜   改 title / 取 tag text
document.title="XXX " + HTMLDOMObject.innerText; 

沒有留言:

張貼留言