最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to detect current page media type in jQuery - Stack Overflow

programmeradmin2浏览0评论

I have a page with input fields. I want to do hide the all 0 values text fields when page is in media type print.

I have tried in jQuery. But this is works both in screen mode and print model.

HTML:

<div class="screen">some screen</div>
<div class="print">some print</div>
<input type='text' name='' id='1' class='' value='0'/>
<input type='text' name='' id='2' class='' value='5'/>
<button>Print</button>

JS:

$('button').click(function(){
    $('input[type=text]').each(function(){
      if ( $(this).val() == 0 ){
        $(this).hide()
      }     
    })
})

CSS:

@media print{
    .screen{
      display:none;
    }

    .print{
      display:block;  
    }
}

@media screen{
    .screen{
      display:block        
    }

    .print{
      display:none;   
    }
}

If I detect the current page's media type. I can finished it. Unfortunately I couldn't get the right code.

I also tried jmediatype, but there is no download option.

I have a page with input fields. I want to do hide the all 0 values text fields when page is in media type print.

I have tried in jQuery. But this is works both in screen mode and print model.

HTML:

<div class="screen">some screen</div>
<div class="print">some print</div>
<input type='text' name='' id='1' class='' value='0'/>
<input type='text' name='' id='2' class='' value='5'/>
<button>Print</button>

JS:

$('button').click(function(){
    $('input[type=text]').each(function(){
      if ( $(this).val() == 0 ){
        $(this).hide()
      }     
    })
})

CSS:

@media print{
    .screen{
      display:none;
    }

    .print{
      display:block;  
    }
}

@media screen{
    .screen{
      display:block        
    }

    .print{
      display:none;   
    }
}

If I detect the current page's media type. I can finished it. Unfortunately I couldn't get the right code.

I also tried jmediatype, but there is no download option.

Share edited Feb 23, 2018 at 14:41 RLesur 5,9101 gold badge19 silver badges54 bronze badges asked Oct 19, 2012 at 3:06 KKKKKK 1,6627 gold badges29 silver badges50 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

If you just want to hide the fields with value="0", you can do this with just CSS:

@media print {
    input[value="0"] {
        display: none;
    }
}

An alternative would be to give those elements a class that only gets hidden by the print stylesheet:

$('button').click(function(){
    $('input[type=text]').filter(function() {
        return parseInt(this.value, 10) == 0;
    }).addClass('print-hidden');
});

And use a style like this:

@media print {
    .print-hidden {
        display: none;
    }
}

Define a CSS file for media print only which contain:

.empty
{
    display: none;
}

include this CSS file in your mage, only for media print

Add a listener on the button for the click event. This listener will add empty CSS class for input with empty value.

$("ID_OF_YOUR_BUTTON").click(function(mouseEvent) {

    $("input[type=text]").each(function(index, element) {

        if ($(element).val().length == 0)
            $(element).addClass("empty");

    });
});

For the media screen, this class will do nothing, but for the media print it will change the display to none.

If you want to find out the media type of a particular stylesheet you can do this in jQuery:

var media = $('link[href$="styles.css"]').attr('media');

You'd have to separate your stylesheets and load them in the markup with the media attribute instead of using the media queries in one file.

发布评论

评论列表(0)

  1. 暂无评论
ok 不同模板 switch ($forum['model']) { /*case '0': include _include(APP_PATH . 'view/htm/read.htm'); break;*/ default: include _include(theme_load('read', $fid)); break; } } break; case '10': // 主题外链 / thread external link http_location(htmlspecialchars_decode(trim($thread['description']))); break; case '11': // 单页 / single page $attachlist = array(); $imagelist = array(); $thread['filelist'] = array(); $threadlist = NULL; $thread['files'] > 0 and list($attachlist, $imagelist, $thread['filelist']) = well_attach_find_by_tid($tid); $data = data_read_cache($tid); empty($data) and message(-1, lang('data_malformation')); $tidlist = $forum['threads'] ? page_find_by_fid($fid, $page, $pagesize) : NULL; if ($tidlist) { $tidarr = arrlist_values($tidlist, 'tid'); $threadlist = well_thread_find($tidarr, $pagesize); // 按之前tidlist排序 $threadlist = array2_sort_key($threadlist, $tidlist, 'tid'); } $allowpost = forum_access_user($fid, $gid, 'allowpost'); $allowupdate = forum_access_mod($fid, $gid, 'allowupdate'); $allowdelete = forum_access_mod($fid, $gid, 'allowdelete'); $access = array('allowpost' => $allowpost, 'allowupdate' => $allowupdate, 'allowdelete' => $allowdelete); $header['title'] = $thread['subject']; $header['mobile_link'] = $thread['url']; $header['keywords'] = $thread['keyword'] ? $thread['keyword'] : $thread['subject']; $header['description'] = $thread['description'] ? $thread['description'] : $thread['brief']; $_SESSION['fid'] = $fid; if ($ajax) { empty($conf['api_on']) and message(0, lang('closed')); $apilist['header'] = $header; $apilist['extra'] = $extra; $apilist['access'] = $access; $apilist['thread'] = well_thread_safe_info($thread); $apilist['thread_data'] = $data; $apilist['forum'] = $forum; $apilist['imagelist'] = $imagelist; $apilist['filelist'] = $thread['filelist']; $apilist['threadlist'] = $threadlist; message(0, $apilist); } else { include _include(theme_load('single_page', $fid)); } break; default: message(-1, lang('data_malformation')); break; } ?>