javascript - 更改视口(viewport)上的元素类型

标签 javascript html css viewport

首先,我什至不确定这是否可能。当视口(viewport)低于 500 像素时,我试图更改元素的 HTML 元素类型。这样做的原因是因为我的元素有一个多页 PDF(而不是蜘蛛猴)。有问题的编码如下:

HTML

<title>Agony's Awesome Spot</title>
<link rel="icon" type="image/png" href="images/yay.png">
<link rel="stylesheet" href="index.css">
<link rel="stylesheet" type="text/css" media="only screen and (min-width:50px) and (max-width:500px)" href="small.css">
<link rel="stylesheet" type="text/css" media="only screen and (min-width:501px) and (max-width:800px)" href="medium.css">
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<script src="http://code.jquery.com/jquery-1.8.3.min.js"></script>
<script src="jquery-ui-1.8.23.custom.min.js"></script>
<script src="faqs.js"></script>
<h2>Feats and/or Strengths</h2>
    <div>
        <p>Here is my StackOverflow Profile:<br></br>
            <a href="http://stackoverflow.com/users/2886993/agony">
            <img src="http://stackoverflow.com/users/flair/2886993.png" width="208" height="58" alt="profile for Agony at Stack Overflow, Q&amp;A for professional and enthusiast programmers" title="profile for Agony at Stack Overflow, Q&amp;A for professional and enthusiast programmers"></a>
        <br></br>Get my resumé here:
        <br></br>
        <embed id="resume" height="900px" width="900px" src="http://images.nationalgeographic.com/wpf/media-live/photos/000/007/cache/spider-monkey_719_600x450.jpg">
        </p>
    </div>

我试图链接到 <script>标签基于 media="only screen and (min-width:50px) and (max-width:500px)" ,但这没有用。基本上我想要发生的是当用户的视口(viewport)小于 500px 时,<embed>变成 <a>标签代替。我可以相应地更改 CSS 而不会出现问题,但我不确定如何完全更改元素标签。

我不确定如何获得 JSFiddle 来演示这一点,所以我发布了网站 here .

如有任何帮助,我们将不胜感激。

最佳答案

添加两个标签并操作它们的 display 属性

<a id="myAnchor" ...>...</a>
<embed id="resume" height="900px" width="900px" src="http://images.nationalgeographic.com/wpf/media-live/photos/000/007/cache/spider-monkey_719_600x450.jpg">

和CSS:

#resume {
    display: none;
}

@media screen and(min-width: 500px) {
    #resume {
        display: block;
    }
    #myAnchor {
        display: none;
    }
}

关于javascript - 更改视口(viewport)上的元素类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20073250/

相关文章:

javascript - 将音频从 nodejs 流式传输到 html5 音频标签

javascript - 当您在 iOS 中将 Web 应用程序添加到主屏幕时,您将无法再存储 cookie

javascript - 无法在超链接的 div 中单击 img

JavaScript 和隐藏 div 不起作用

css - 转换和滑入动画在 Chrome 中不起作用

javascript - 如何获取 HTML 元素的背景颜色?

javascript - 以设定的间隔增加和减少 div 的宽度和高度

javascript - 如果焦点不在输入上,则隐藏搜索结果

css - HTML/CSS : Hanging section numbers to the left of left-aligned heading text

javascript - 如何在jquery中取消选中嵌套复选框并保留当前单击的复选框?