Jquery对javascript的转换帮助

时间:2010-04-01 18:15:02

标签: javascript jquery

我正在尝试将以下jquery转换为javascript语法,但不熟悉其中之一,我遇到了问题

var div = $('<div style="width:50px;height:50px;overflow:hidden;position:absolute;top:-200px;left:-200px;"><div style="height:100px;"></div>');
// Append our div, do our calculation and then remove it
$('body').append(div);
var w1 = $('div', div).innerWidth();
div.css('overflow-y', 'scroll');
var w2 = $('div', div).innerWidth();
$(div).remove();

2 个答案:

答案 0 :(得分:4)

您无法直接将jQuery转换为本机JavaScript,因为它为您无法解释的各种变量(浏览器,浏览器版本,操作系统等)提供了不同的技巧。

你可以尝试这样做,但这会浪费你的时间。顶级JavaScript专业人员正在研究这些框架,以使您更轻松地使用JavaScript。不要试图通过自己的努力来解除他们的辛勤工作......

答案 1 :(得分:1)

function scrollbarWidth2() {
    var outterDiv = document.createElement('div');
    var innerDiv = document.createElement('div');
    outterDiv.style.cssText = "width:50px;height:50px;overflow:hidden; background:red; position:absolute;top:200px;left:200px;";
    innerDiv.style.cssText = "height:100px;";
    outterDiv.appendChild(innerDiv); 
    document.body.appendChild(outterDiv);
    var w1 = innerDiv.offsetWidth;
    outterDiv.style.overflowY = "scroll";
    var w2 = innerDiv.offsetWidth;
    document.body.removeChild(outterDiv);
    return (w1 - w2);
}