gpt4 book ai didi

javascript - 如何根据浏览器窗口的宽度输出 HTML?

转载 作者:行者123 更新时间:2023-11-30 08:42:38 26 4
gpt4 key购买 nike

我制作了这个 javascript 并尝试过但没有成功。
它必须显示广告横幅。
如果我把这个 iframe 放在 javascript 标签的外面,它工作正常,但我确实放在 javascript 标签里面,因为 iframe 将取决于浏览器窗口的宽度。

我怎样才能使这个工作????

<head>

<script type="text/javascript">
width = jQuery(".pagenator").width();
if (width > 730) {
<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='728' height='90' src='http://foofoofoofootest.com/adspot.aspx?id=21442?'>
</iframe>
} else if (width > 470) {
<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='468' height='60' src='http://foofoofoofootest.com/adspot.aspx?id=21443?'>
</iframe>
} else if (width > 255) {
<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='250' height='250' src='http://foofoofoofootest.com/adspot.aspx?id=21444?'>
</iframe>
} else {
<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='200' height='200' src='http://foofoofoofootest.com/adspot.aspx?id=21445?'>
</iframe>
}
</script>


<head>

<body>



</body>

</html>

更新

<body>

<script type="text/javascript">
var TheHTML = "";
width = jQuery(".pagenator").width();
if (width > 730) {
TheHTML = "<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='728' height='90' src='http://foofoofoofootest.com/adspot.aspx?id=21442'></iframe>";
} else if (width > 470) {
TheHTML = "<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='468' height='60' src='http://foofoofoofootest.com/adspot.aspx?id=21443'></iframe>";
} else if (width > 255) {
TheHTML = "<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='250' height='250' src='http://foofoofoofootest.com/adspot.aspx?id=21444'></iframe>";
} else {
TheHTML = "<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='200' height='200' src='http://foofoofoofootest.com/adspot.aspx?id=21445'></iframe>";
}
$("#frameHolder").html(TheHTML);
</script>


<div id="frameHolder"></div>


</body>

最佳答案

您需要将 HTML 添加到 DOM;像这样:

var TheHTML = "";
var width = jQuery(".pagenator").width(); // add a var, otherwise you're creating a global
if (width > 730) {
TheHTML = "<iframe frameborder='0' allowtransparency='true' marginheight='0' marginwidth='0' scrolling='no' width='728' height='90' src='http://foofoofoofootest.com/adspot.aspx?id=21442?'></iframe>";
} else if (width > 470) { ....

$('ContainerOfTheIframe').html(TheHTML);

这应该有效。还要确保在脚本运行时可以使用 jQuery(即,将 jquery.js 文件的引用放在脚本上方)。

关于javascript - 如何根据浏览器窗口的宽度输出 HTML?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24868474/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com