2012-01-23 28 views
0

我有一個應用程序,我使用的是jQuery + maphilight ,除了突出顯示圖像地圖片段之外,我還想根據鼠標懸停/圖像映射的鼠標事件。jquery:在運行時創建/讀取.css樣式

我知道如何去做所有的事情(只需對addClass和removeClass進行適當的調用,以使我想突出顯示的HTML元素)除了一件事。我想做以下任一操作:

  • 在運行時,反映了maphilight視覺設置創建CSS樣式(線/填充樣式+不透明度)
  • 設置maphilight插件來反映,我有一個CSS樣式建立。

我寧願做後者並將其配置在.css文件中,但目前我不知道該怎麼做。

任何人都可以幫助我嗎?


編輯:我在尋找,包括如何從jQuery插件maphilight讀或寫的風格,因爲它沒有那麼多的文檔的答案。

回答

0

它只能改變標題樣式表。正確使用時,這種技術更勝一籌。它不應該取代適當的班級作業。

var renderDropArea = function(renderMe) { 
    var dropSheet = document.getElementById('dropAllowedDisplay'); 
    if (renderMe) { 
    // IE 7 & 8 
    if (dropSheet.styleSheet) { 
     dropSheet.styleSheet.addRule('.dropAllowedHead','background-color:#72F193;'); 
     dropSheet.styleSheet.addRule('.dropAllowed','background-color:#72F193;'); 
    } else { 
     document.getElementById('dropAllowedDisplay').innerHTML = 
     ".dropAllowedHead { background-color:#72F193; } .dropAllowed { background-color:#72F193; }"; 
    } 
    } else { 
    // IE 7 & 8 
    if (dropSheet.styleSheet) { 
     if (dropSheet.styleSheet.rules.length) { 
     dropSheet.styleSheet.removeRule(0); 
     dropSheet.styleSheet.removeRule(0); 
      } 
    } else { 
     document.getElementById('dropAllowedDisplay').innerHTML = ''; 
    } 
     } 
} 
+0

這有助於我瞭解,有發生潛在的CSS DOM操作,但我更感興趣的是如何使用jQuery做,因爲使用該庫的理由應該是添加瀏覽器獨立性並使其更易於使用。 –

+0

我不相信有任何實現可以自動處理這個。你總是可以把我的這段代碼放到一個jQuery小部件中。這是jQuery的真正威力。所有的「瀏覽器獨立」都源於社區貢獻。這是一個這樣的貢獻http://forum.jquery.com/topic/jquery-creating-stylesheets-in-jquery –