2013-04-26 95 views
0

的某些屬性的某些值如何我,用JavaScript,查看網頁是否具有以下任何一項:HTML解析/檢查某些標籤

<meta name="viewport" content="width=device-width, initial-scale=1.0 maximum-scale=1, user-scalable=no" /> 

<meta name="viewport" content="initial-scale=1.0 maximum-scale=1" /> 

<meta name="viewport" content="maximum-scale=1" /> 

<meta name="viewport" content="maximum-scale=1.0" /> 

<meta name="viewport" content="user-scalable=no" /> 

,如果任何的,這是源,然後運行某些JavaScript代碼,就像在if條件套件中一樣。

基本上我試圖做的是檢查縮放是否被禁用無論如何,如果是這樣,執行某些代碼,如果不是執行其他代碼。

+1

VAR節點= $( '元[NAME =視]') ; (節點).each(function(index,nodeValue)var content = $(nodeValue).attr('content'); //在此檢查nodeValue }); – vijay 2013-04-26 07:58:13

+0

@VijayPatel謝謝。 – 2013-04-26 08:06:19

+0

@VijayPatel你能解釋一下如何檢查索引中每個元素的nodeValue嗎? – 2013-04-26 08:08:20

回答

0

您可以使用

function readViewportValues(){ 
    var metas = $('meta[name=viewport]'); 
    var properties = {}; 

    function isNumber(n) { 
     return !isNaN(parseFloat(n)) && isFinite(n); 
    } 

    function readItem(index, text) { 
     var parts = jQuery.trim(text).split(' '); 
     console.log(parts); 
     if (parts.length > 1) { 
      $.each(parts, readItem); 
     } else { 
      parts = parts[0].split('='); 
      var key = jQuery.trim(parts[0]), 
       value = parts[1]; 
      value = isNumber(value) ? parseFloat(value) : value; 
      properties[key] = value; 
     } 
    } 

    metas.each(function() { 
     var content = $(this).attr('content').split(','); 
     $.each(content, readItem); 
    }); 
    return properties; 
} 


// usage 
var viewport = readViewportValues(); 
if (viewport['maximum-scale'] === 1){ 
    // do something 
} 

這將返回一個對象與發現性,你可以查詢它..

0
var nodes = $('meta[name=viewport]'); 
$(nodes).each(function(index, nodeValue){ 
    var content = $(nodeValue).attr('content');// check nodeValue here 
    // You will get values of content from each meta element here in content variable ex. "user-scalable=no" 
    // so now based on these values you can so do any processing you want 
});