英文:
How can I get attribute of a div from WebView
问题
我有一个网页,其中包含
<div class="mydiv" style="width: 50%"> div content </div>
我想获取宽度值。
英文:
I have a web page that has
<div class="mydiv" style="width: 50%"> div content </div>
and I want to get the width value
答案1
得分: 1
<!-- 开始代码片段:js 隐藏:false 控制台:true Babel:false -->
<!-- 语言:lang-js -->
var elements = document.getElementsByClassName('mydiv');
console.log(elements[0].style.width);
<!-- 语言:lang-html -->
<div class="mydiv" style="width: 50%"> div content </div>
<!-- 结束代码片段 -->
英文:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var elements = document.getElementsByClassName('mydiv');
console.log(elements[0].style.width);
<!-- language: lang-html -->
<div class="mydiv" style="width: 50%"> div content </div>
<!-- end snippet -->
答案2
得分: 0
50%
英文:
To get the width from the below div,
<div class="mydiv" style="width: 50%"> div content </div>
Please try,
<!DOCTYPE html>
<html>
<head>
<title>
</title>
</head>
<body>
<div class="mydiv" style="width: 50%"> div content </div>
<script>
window.addEventListener('load', (event) => {
console.log('page is fully loaded');
var elements = document.getElementsByClassName('mydiv');
var st = elements[0].getAttribute('style');
var width = st.split(':')[1].trim();
console.log(width);
});
</script>
</body>
</html>
Output in console:
page is fully loaded
50%
答案3
得分: 0
你可以使用以下代码:
var m = document.getElementById("mydiv").offsetWidth;
同样,如果你想知道窗口的宽度和高度,可以使用以下代码:
var w = window.innerWidth;
var h = window.innerHeight;
英文:
you can use
var m=document.getElementById("mydiv").offsetWidth;
Similarly if you want to know about window width and height you can use
var w = window.innerWidth;
var h = window.innerHeight;
答案4
得分: 0
你可以在你的JavaScript中尝试这个。
console.log(document.querySelector(".mydiv").style.width);
英文:
You can try this in your js.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
console.log(document.querySelector(".mydiv").style.width);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论