dhtml

How do I retrieve an HTML element's actual width and height?

允我心安 提交于 2019-11-25 22:32:58
问题 Suppose that I have a <div> that I wish to center in the browser\'s display (viewport). To do so, I need to calculate the width and height of the <div> element. What should I use? Please include information on browser compatibility. 回答1: You should use the .offsetWidth and .offsetHeight properties. Note they belong to the element, not .style . var width = document.getElementById('foo').offsetWidth; 回答2: Take a look at Element.getBoundingClientRect(). This method will return an object

How to load up CSS files using Javascript?

廉价感情. 提交于 2019-11-25 22:18:17
问题 Is it possible to import css stylesheets into a html page using Javascript? If so, how can it be done? P.S the javascript will be hosted on my site, but I want users to be able to put in the <head> tag of their website, and it should be able to import a css file hosted on my server into the current web page. (both the css file and the javascript file will be hosted on my server). 回答1: Here's the "old school" way of doing it, which hopefully works across all browsers. In theory, you would use

How do I attach events to dynamic HTML elements with jQuery? [duplicate]

给你一囗甜甜゛ 提交于 2019-11-25 22:13:23
问题 This question already has an answer here: Event binding on dynamically created elements? 23 answers Suppose I have some jQuery code that attaches an event handler to all elements with class .myclass . For example: $(function(){ $(\".myclass\").click( function() { // do something }); }); And my HTML might be as follows: <a class=\"myclass\" href=\"#\">test1</a> <a class=\"myclass\" href=\"#\">test2</a> <a class=\"myclass\" href=\"#\">test3</a> That works with no problem. However, consider if