简体   繁体   English

在div或窗口大小上更改段落内容

[英]Changing paragraph content on div or window size

I'm trying to edit the content of a paragraph based on window / div size but didn't have success yet.. Here is what I've tried : 我正在尝试根据window / div大小编辑段落的内容,但是还没有成功。.这是我尝试过的事情:

$(document).ready(function(){
    function check(){
        var w = $(window).width();
        var ot = "elegant@elegant.com<br>commercial@elegant.com";
        var nt = "e@elegant.com<br>c@elegant.com";  
        w < 768 ? $(".mail").text.parseHTML(nt) : $(".mail").text.parseHTML(ot);
    }
    check();
    $(window).resize(check);
    });

anyone got any clues on how to proceed ? 任何人都知道如何进行的线索?

I would do something like: 我会做类似的事情:

<p class="ot">elegant@elegant.com<br>commercial@elegant.com</p>
<p class="nt">e@elegant.com<br>c@elegant.com</p>

<style>
    p.ot { display: block; }
    p.nt { display: none; }
    @media screen and (max-width: 768px) {
        p.ot { display: none; }
        p.nt { display: block; }
    }
</style>

http://jsfiddle.net/5eg499qm/ http://jsfiddle.net/5eg499qm/

It seems that you're trying to insert parsed HTML into an element. 似乎您正在尝试将已解析的HTML插入元素。

The strings you want to parse are not yet in the DOM -- they are just javascript variables -- so you'll want the calls to parseHTML() to be performed on those strings rather than text from a DOM element. 您要解析的字符串尚不在DOM中,它们只是javascript变量,因此您希望对这些字符串执行parseHTML()的调用,而不是DOM元素中的文本。

Also, since you are inserting HTML, use the html() method rather than text() . 另外,由于要插入HTML,因此请使用html()方法,而不要使用text()

The correct syntax is: 正确的语法是:

$(".mail").html($.parseHTML(nt));
$(".mail").html($.parseHTML(ot));

References: parseHTML() , html() 参考: parseHTML()html()

WORKING EXAMPLE 工作实例


That being said, I would use a CSS option as suggested by Rich Remer. 话虽如此,我将使用Rich Remer建议的CSS选项。 CSS is generally used for layout while javascript is generally used for interaction. CSS通常用于布局,而javascript通常用于交互。 Read more . 阅读更多

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

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