[英]How can count all images on a page with JavaScript (including CSS and img)
[英]How can I link CSS parameters to a javascript img?
我在网页上添加了带有Javascript的图像,但是我不知道如何修改或放置它。 如何将CSS链接到图像,或者在没有CSS的情况下可以对其进行修改?
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>PingPongKép</title>
<link rel="stylesheet" href="CSS/style.css">
</head>
<body>
<script>
function ilonaKep() {
var img = new Image();
img.src = 'img/ilona.jpg';
document.body.appendChild(img);
}
</script>
<p>Let's See the image
<script>
ilonaKep();
</script>
</p>
</body>
</html>
jQuery使这变得非常容易。 在图像的style
引号中将所需的任何样式应用于图像
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
</head>
<body>
<p> Lets see the image: </p>
<script>
window.onload = function(){
addImage();
function addImage() {
$('body').append('<img style="any css styling in here" src="img/ilona.jpg">');
}
}
</script>
</body>
</html>
Javascript可以设置元素的属性,
img.setAttribute("style", "background-color: red;");
https://www.w3schools.com/jsref/met_element_setattribute.asp
另外,如果您不想使用内联CSS,则可以将CSS添加到CSS文件中,并使用jquery为图像提供一个类
$("img").addClass("myImage");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.