简体   繁体   English

使用 JavaScript 更改元素上的 CSS?

[英]Change CSS on element with JavaScript?

I'm new to frontend development, and I want to rezize an <img> with JavaScript:我是前端开发的新手,我想用 JavaScript 调整<img>

Which element?哪个元素?

<div class="dz-image">
    <img data-dz-thumbnail="" alt="principal.png" src="http://localhost:49407/file/Image/660">
</div>

What function im using in js?我在 js 中使用了什么函数?

this.emit("thumbnail", mockFile, "http://localhost:11111/file/Image/" + principal)
    { document.querySelector('data-dz-thumbnail') }; 

I'm using querySelector to get the spefic <img> element.我正在使用querySelector来获取特定的<img>元素。

Is it possible to add some style directly on that line like height:120; width: 120;是否可以直接在该行上添加一些样式,例如height:120; width: 120; height:120; width: 120; ? ?

Yes,it is possible to add some style directly.是的,可以直接添加一些样式。

let ele = document.querySelector('[data-dz-thumbnail]')
ele.style.width="120px"
ele.style.height="120px"

Per my understanding, queryselector should be classname , id or tag根据我的理解, queryselector应该是classname , id 或 tag

Second you can add css by taking a variable for your querySelector line.其次,您可以通过为querySelector行获取变量来添加 css。 It is like:它像是:

var el = document.querySelector('div'); 
el.style.backgroundColor = 'green';
el.style.display = 'none';
el.style['border-radius'] = '5px';

I solved this using jQuery's .css() method like this:我使用 jQuery 的.css()方法解决了这个问题,如下所示:

$('[data-dz-thumbnail]').css('width', '120');
$('[data-dz-thumbnail]').css('height', '120');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM