<p id ="myP">
Render this text using the myC class </p>
---------------------------
.teststyle {
background-color:#00ff00;
font-size: 200%;
}
---------------------
window.onload = load;
function load(){
document.getElementById("myP").className = "teststyle";
}
----------------------
Run Code Online (Sandbox Code Playgroud)
使用jsfiddle.net和JavaScript我发现JS中不支持"getElementByClass".我想知道为什么?如果我的XTHML代码中有一个类,如何转换此代码怎么办?
我注意到的另一个奇怪的事情是:如果我onload = "load()"在xhtml中使用将不会像我"windows.load"在js-code中那样工作......为什么会这样?它应该是一样的,也许只是我......谢谢!
如何在使用函数指针传入参数的同时调用Javascript中的函数?
例:
function foo (a, callback) {
jQuery.post('/soon/check.json', { var:a }, function(resp) {
callback(resp);
});
}
function process_json(resp) {
// Do something with resp
}
foo(bar, process_json);
Run Code Online (Sandbox Code Playgroud)
永远不会调用"process_json".在Firebug中,字符串"process_json"被传递到"foo",但我认为它代表了一个指向函数"process_json"的指针.
在Javascript中,是不是可以通过指针调用函数并传入参数?
伙计我试图在onbeforeunload事件上调用此确认函数,但它没有被调用.我在这里错过了什么?
我在body标签中调用它:<body onbeforeunload="confirmation();">
我也尝试过:<body onbeforeunload="return confirmation();">
码:
function confirmation(){
var answer = confirm('You have unsaved changes!!');
if(answer == true)
{
alert('Bye Bye!!!');
}
else{
alert('You are staying');
return false;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个带有表格的index.html页面:
<body>
<table class="mytable">
<tr id="1">
<td>John</td>
<td>123</td>
</tr>
<tr id="2">
<td>Kate</td>
<td>456</td>
</tr>
</table>
<script src="my.js"></script>
</body>
Run Code Online (Sandbox Code Playgroud)
我已将my.js包含在我的页面中,我试图在my.js中以下列方式获取鼠标单击行的id:
$('.mytable tr').click(function(e){
alert('clicked.'); //even did not get alert here.
var rowId = $(this).attr('id');
alert(rowId);
});
Run Code Online (Sandbox Code Playgroud)
但我没有得到点击行的行ID(没有警报),我在哪里错了?
- -解 - -
我的坏,我有一个地方清空了桌子,这意味着没有tr.这就是为什么它不起作用.很抱歉打扰.
定义JavaScript调用时,如下所示:
<input type="button" onclick="someJavaScriptFunction" />
Run Code Online (Sandbox Code Playgroud)
写实际通话的最佳方式是什么?我见过很多方法:
javascript:someJavascriptFunction();
Run Code Online (Sandbox Code Playgroud)
要么
justSomeJavaScriptFunction();
Run Code Online (Sandbox Code Playgroud)
要么
withoutTheSemicolon()
Run Code Online (Sandbox Code Playgroud)
任何方法都有任何好处或警告吗?我意识到addEventListener在某些情况下使用更灵活.
window.addEventListener('load', function () {
gBrowser.addEventListener('DOMContentLoaded', function () {
// stuff that happens for each web page load goes here
}, false),false)
Run Code Online (Sandbox Code Playgroud)
我对此代码有以下问题:DOMContentLoaded在某些页面上多次触发.
我正在为复杂的Web应用程序编写用户脚本.现有代码正在捕获'j'和'k'keydown事件.
我希望能够找到这个功能,看看它在做什么.有没有办法列出文档中的所有关键事件处理程序?或者也许是一种在Chrome开发者工具中以某种方式设置断点的方法,当我按下这封信时?
我正在使用JQuery并尝试使用delegate该hover操作.问题是悬停操作可以获得两个处理程序,句柄和句柄输出.如何使用委托来实现这一目标?
我试过这个并没有用:
$(document).delegate('.box', 'hover',
function() { $(".a").addClass(".hover");},
function() { $(".a").removeClass(".hover");});
Run Code Online (Sandbox Code Playgroud) 以下页面显示五个按钮,单击每个按钮获取警报'5'.我想当我点击第一个按钮时,我得到警报'1',第二个按钮'2'......等等.
<!doctype html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script>
</head>
<body>
<div id="nums"></div>
<script type="text/javascript">
var nums = [1,2,3,4,5];
for(var i in nums){
var num = nums[i];
var btn=$('<button></button>').text(num).click(function(){alert(num);});
$('#nums').append(btn);
}
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud) 我已经使用以下内容填充浏览器窗口中的webGL内容:
CSS:
canvas
{
float: left;
width: 100%;
height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
并且:
function webGLStart() {
var canvas = document.getElementById("cool3D");
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
...
Run Code Online (Sandbox Code Playgroud)
到目前为止,这对于保持完全覆盖是有效的,没有窗口大小或全屏.问题是指定相机的纵横比以避免失真.
如何将其设置为〜"window.innerWidth/window.innerHeight"?OnWindowResize?