简体   繁体   English

立即使用 javascript 设置 HTML 元素宽度属性

[英]Set a HTML elements width attribute right away using javascript

Is it possible to set an elements width like this:是否可以像这样设置元素宽度:

<img id="backgroundImg" src="images/background.png" alt="" width="javascript:getScreenSize()['width']+px" height="1100px"/>

Where getScreenSize() is a javascript function.其中 getScreenSize() 是 javascript function。

        function getScreenSize()
        {
            var res = {"width": 630, "height": 460};

            if (document.body) 
            {
                 if (document.body.offsetWidth)  res["width"]  = document.body.offsetWidth;
                 if (document.body.offsetHeight) res["height"] = document.body.offsetHeight;
            }

            if (window.innerWidth)  res["width"]  = window.innerWidth;
            if (window.innerHeight) res["height"] = window.innerHeight;
            alert( res['width'] );

            return res;
        }

Or do I have to change the img's width inside a javascript function?还是我必须在 javascript function 中更改 img 的宽度?

function changeImgWidth( img )
{
   img.offsetRight = getScreenWidth()['width'] + "px";
}

You can't do it with that syntax, no.你不能用那种语法来做,不。 You could use document.write to do it, but I suspect you'd be better off doing it after the DOM loads, because I think your getScreenSize function may not report correct results until then (but it may, you'll have to test).您可以使用document.write来执行此操作,但我怀疑您最好在 DOM 加载后执行此操作,因为我认为您的getScreenSize function 在那之前可能不会报告正确的结果(但它可能,您必须测试)。

To do it with document.write :document.write做到这一点:

<script>
document.write('<img id="backgroundImg" src="images/background.png" alt="" width="' + getScreenSize()['width'] + '" height="1100px"/>');
</script>

To do it once the DOM is loaded, put this at the very end of your page, just before the closing </body> tag:要在加载 DOM 后执行此操作,请将其放在页面的最后,就在结束</body>标记之前:

<script>
document.getElementById('backgroundImg').width = getScreenSize()['width'];
</script>

In both of those examples, I've stuck with the width attribute (note that you don't use a units suffix — "px" — with the width attribute).在这两个示例中,我都坚持使用width属性(请注意,您没有使用单位后缀 - “px” - 与width属性一起使用)。 But you might consider using the CSS width property instead (which you would use the suffix with).但是您可能会考虑使用 CSS width属性(您可以使用后缀)。


Stepping back, though, rather than relying on JavaScript to set the width, I'd recommend trying to do it with CSS.不过,退后一步,而不是依靠 JavaScript 来设置宽度,我建议尝试使用 CSS 来设置。 You can make the image 100% of the width of its container by using style='width: 100%' within the tag or:您可以通过在标签中使用style='width: 100%'或:

#backgroundImg {
    width: 100%;
}

...in a stylesheet. ...在样式表中。

you don't need +px in the img-tag您不需要在 img-tag 中添加 +px

Even if it could be done that way, it would be better not to, so you can separate content(HTML) & behavior(javascript).即使可以这样做,最好不要这样做,因此您可以将内容(HTML)和行为(javascript)分开。 It's the same reason why it's better not to put这也是为什么最好不要放的原因

onclick='somefunction(); return false;'

inside a link, for example.例如,在链接内。 Instead, if you used jQuery, for example, you could put相反,如果你使用 jQuery,例如,你可以把

$('#somelink').click(function() {//do something});

inside a里面一个

$(document).ready(

block.堵塞。 If nothing else, you can have all the javascript in one place, instead of scattered everywhere in the markup.如果不出意外,您可以将所有 javascript 放在一个地方,而不是分散在标记中的任何地方。 CSS stylesheets are better than inline styles for the same reason.出于同样的原因,CSS 样式表优于内联 styles。

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

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