오락기/js

jQuery attribute

문방구앞오락기 2017. 4. 18. 10:39

<a id="target" href="http://opentutorials.org">opentutorials</a>

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

<script>

var t = $('#target'); //id target 엘리먼트  

console.log(t.attr('href')); //http://opentutorials.org // get

t.attr('title', 'opentutorials.org'); // title 속성의 값을 설정한다. //set

t.removeAttr('title'); // title 속성을 제거한다. 

</script>






<body>

<a id="t1" href="./demo.html">opentutorials</a>

<input id="t2" type="checkbox" checked="checked" />

<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>

<script>

// 현재 문서의 URL이 아래와 같다고 했을 때

// http://localhost/jQuery_attribute_api/demo2.html

var t1 = $('#t1');

console.log(t1.attr('href')); // ./demo.html 

console.log(t1.prop('href')); // http://localhost/jQuery_attribute_api/demo.html 

 

var t2 = $('#t2');

console.log(t2.attr('checked')); // checked

console.log(t2.prop('checked')); // true

</script>


결과
./demo.html
(index):16 http://localhost:9081/home/demo.html
(index):19 checked
(index):20 true