문제

how can I get only the link?

<a id="skip" href="http://google.de">

This doenst work:

var doc = document.getElementById('skip');
var array = doc ? doc.getElementsByTagName('a')  : [
];
if (array.length > 2)
array[0].href = array[1].href;
alert(array[0]);
도움이 되었습니까?

해결책

Your variable doc will never be an array as document.getElementById will only return the matched element, or undefined.

This code is all you need:

var doc = document.getElementById('skip');
alert(doc.href);

Extra:

This conditional statement would return the element, and never be false:

var array = doc ? doc.getElementsByTagName('a') : [];

This would always return false as the html element does not have a .length property:

if(array.length > 2)

This will cause an error as array is not an array.

alert(array[0]);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top