admin管理员组文章数量:1431037
I'm trying to create a script on a website that takes the user's location and displays the results. I have a problem with the setting and styles. How can i edit style or how can i edit the code so that it can work with css styles in <div>
my code:
<pre></pre>
<script>
const address = document.querySelector("pre");
function onError() {
address.innerText = "";
}
async function onSuccess(position) {
const geocode = await fetch(`/${position.coords.latitude},${position.coords.longitude}?json=1`);
const geoResponse = await geocode.json();
address.innerText = `${geoResponse.country}, ${geoResponse.city}, ${geoResponse.postal}, ${geoResponse.staddress}`;
}
if (!navigator.geolocation) {
onError();
} else {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
</script>
I'm trying to create a script on a website that takes the user's location and displays the results. I have a problem with the setting and styles. How can i edit style or how can i edit the code so that it can work with css styles in <div>
my code:
<pre></pre>
<script>
const address = document.querySelector("pre");
function onError() {
address.innerText = "";
}
async function onSuccess(position) {
const geocode = await fetch(`https://geocode.xyz/${position.coords.latitude},${position.coords.longitude}?json=1`);
const geoResponse = await geocode.json();
address.innerText = `${geoResponse.country}, ${geoResponse.city}, ${geoResponse.postal}, ${geoResponse.staddress}`;
}
if (!navigator.geolocation) {
onError();
} else {
navigator.geolocation.getCurrentPosition(onSuccess, onError);
}
</script>
Share
Improve this question
asked Dec 3, 2020 at 16:35
newstudentnewstudent
211 silver badge2 bronze badges
1
- You can review the javascript style – sercan Commented Dec 3, 2020 at 16:40
2 Answers
Reset to default 2I've created a snippet with your code. In this case, we are targeting a pre
element.
const address = document.querySelector("pre");
address.style.backgroundColor = "blue";
address.style.color = "white";
address.style.fontWeight = "900";
pre {
height: 500px;
width: 500px;
}
<pre>This is a pre element</pre>
However, if you wanted to target a class, you would do this:
<div class="pre"></div>
const address = document.querySelector(".pre");
For id you would do this:
<div id="pre"></div>
const address = document.querySelector("#pre");
When you use id or class you can still target your styles the same way:
address.style.backgroundColor = "blue";
address.style.color = "white";
address.style.fontWeight = "900";
More info here.
document.getElementById(id).style.property = new style
in your case:
const address = document.querySelector("pre");
address.style.color= "red"
本文标签: javascriptHow to change style using element documentquerySelectorStack Overflow
版权声明:本文标题:javascript - How to change style using element document.querySelector - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745468589a2659642.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论