Javascript-форум (https://javascript.ru/forum/)
-   Internet Explorer (https://javascript.ru/forum/css-html-internet-explorer/)
-   -   Как получить значение атрибута width в теге img, находящемся в скрытом диве? (https://javascript.ru/forum/css-html-internet-explorer/15965-kak-poluchit-znachenie-atributa-width-v-tege-img-nakhodyashhemsya-v-skrytom-dive.html)

kolyan1983 21.03.2011 12:59

Как получить значение атрибута width в теге img, находящемся в скрытом диве?
 
Дан к примеру вот такой скрытый див с вложенной внутрь картинкой:

<div id="div_id" style="display: none;">
<img id="img_id" src="i/sewing/combined/4001.png" width="990" height="388" />
</div>

javascript код:

document.getElementById('img_id').width

или например на jquery:

$('#img_id').attr('width')

в ИЕ вернёт 0, хотя в теге прописано width="990". Кто-нибудь знает как получить значение атрибута width в ИЕ не делая див видимым?

dmitriymar 21.03.2011 13:04

http://htmlbook.ru/css/display
прочитай что такое-display: none

Matre 21.03.2011 13:14

function getRealSize(element) {
	var clone = element.cloneNode(true);
	clone.style.position = "absolute";
	clone.style.top = "-1000px";
	clone.style.display = "";
	document.body.appendChild(clone);
	var sizes = {
		height: clone.offsetHeight,
		width: clone.offsetWidth
	};
	document.body.removeChild(clone);
	return sizes;
}

alert( getRealSize(document.getElementById('img_id')).width );

kolyan1983 21.03.2011 13:38

Да, спасибо подошло.

рони 21.03.2011 15:14

А если так ? )))
<div id="div_id" style="display: none;">
<img id="img_id" src="http://javascript.ru/forum/images/ca_serenity/misc/logo.gif" width="990" height="388" />
</div>
<script type="text/javascript">
 function getStyle(b, a) {
    if (document.defaultView && document.defaultView.getComputedStyle) {
        return document.defaultView.getComputedStyle(b, "")[a]
    } else if (b.currentStyle) {
        return b.currentStyle[a]
    }
};
alert(getStyle( document.getElementById('img_id'),'width'))
</script>


Часовой пояс GMT +3, время: 11:46.