我在我的JavaScript代码中使用委托事件处理程序(jQuery),因此当单击动态添加按钮时会发生这种情况.
我想知道这有性能上的缺点吗?
// Delegated event handler
$(document).on('click', '#dynamicallyAddedButton', function(){
console.log("Hello");
});
Run Code Online (Sandbox Code Playgroud)
性能方面,它与此相比如何?
// Regular event handler
$("#regularButton").on('click', function(){
console.log("Hello Again");
});
Run Code Online (Sandbox Code Playgroud)
看看jQuery文档,似乎事件总是在DOM树上一直冒泡.这是否意味着进一步嵌套元素,事件需要工作的时间越长?
编辑:使用JavaScript的事件委托而不是jQuery是否有性能优势?问一个类似的问题,答案是有用的.我想知道使用常规事件处理程序和委托事件处理程序之间的区别.链接的问题使得事件似乎不断冒出DOM树.使用委托事件处理程序,事件会冒泡到顶部然后再返回到指定的元素吗?
在常规Java Try-Catch块中使用PreparedStatements时,我可以更改PreparedStatement以在需要时运行不同的查询,如下所示:
String sqlStatement = "update someTable set someValue = true";
try{
PreparedStatement pstmt = con.prepareStatement(sqlStatement);
pstmt.executeUpdate();
/* Here I change the query */
String anotherSqlStatement = "update aDifferentTable set something = false";
pstmt = con.prepareStatement(anotherSqlStatement);
pstmt.executeUpdate();
}
catch(Exception ex){
...
}
Run Code Online (Sandbox Code Playgroud)
使用Java的Try-with-Resources执行此操作的正确方法是什么?这是我尝试过的,但是"无法分配try-with-resources语句的资源pstmt".
try(Connection con = DriverManager.getConnection(someConnection, user, password);
PreparedStatement pstmt = con.prepareStatement(sqlStatement)){
ResultSet rs = pstmt.executeQuery();
....
/* Here I attempt to change the query, but it breaks */
String anotherSqlStatement = "select something from someTable";
pstmt = con.prepareStatement(anotherSqlStatement); …Run Code Online (Sandbox Code Playgroud) 我的 html 有两个类,“相关”和“a”。我想知道如何选择属于这两个类的 < p > 元素。不只是一个或另一个,两者同时进行。有没有办法做到这一点?
这是我的 html:
<h1>Hi</h1>
<h2 class="important">Hi again</h2>
<p class="a">Random unattached paragraph</p>
<div class="relevant">
<p class="a">first</p>
<p class="a">second</p>
<p>third</p>
<p>fourth</p>
<p class="a">fifth</p>
<p class="a">sixth</p>
</div>
Run Code Online (Sandbox Code Playgroud)
因此,在此示例中,我只想选择“第一”、“第二”、“第五”和“第六”,因为它们同时属于这两个类别。
我试过在同一 css 行上选择两者,但它没有选择任何内容:
.a.relevant{
color: red;
}
Run Code Online (Sandbox Code Playgroud)
谢谢你的帮助!
我是 jquery 的新手,只是在尝试学习。我正在尝试将所有 < p> 元素更改为 < button> 元素,并使用与其对应的 p 元素相同的文本。
所以如果我有这个 html:
<p>Hello</p>
<p>Here is a paragraph</p>
<p>And Another</p>
Run Code Online (Sandbox Code Playgroud)
我想把它改成这样:
<button>Hello</button>
<button>Here is a paragraph</button>
<button>And Another</button>
Run Code Online (Sandbox Code Playgroud)
但是,我的 jquery 代码同时选择了所有 < p> 元素的文本,所以我得到了这个:
<button>Hello Here is a paragraph And Another</button>
<button>Hello Here is a paragraph And Another</button>
<button>Hello Here is a paragraph And Another</button>
Run Code Online (Sandbox Code Playgroud)
我究竟做错了什么?这是我的 jquery/javascript:
var ChangeToButton = function () {
$("p").replaceWith ("<button>" + $("this").text() + "</button>");
}
$("button").on("click", function (event) {
ChangeToButton();
});
Run Code Online (Sandbox Code Playgroud)
感谢您对此的任何帮助。
我想知道如何从JavaScript模块中的私有和公共函数访问公共属性?
例如,如果这是我的模块:
var PersonModule = (function(){
var sayHello = function(){
alert("Hello " + name);
}
var privateChangeNameToBob = function(){
this.name = "Bob";
}
var changeName = function(){
privateChangeNameToBob();
}
return {
name: "",
sayHello: sayHello,
changeName: changeName
}
})();
Run Code Online (Sandbox Code Playgroud)
无论sayHello或changeName功能正常工作,因为我不知道如何访问公共name属性.有没有办法做到这一点?我通过使用带有getter和setter的私有变量来解决它,但我想知道如果没有它们我是否可以做到.
在Java中,我经常关闭finally块中的变量,如下所示:
public void someMethod(){
InputStream inStream = null;
PreparedStatement pStatement = null;
try{ do something here }
catch(Exception ex){ do something here }
finally{
try{ if (inStream != null) inStream.close(); } catch(Exception ex){/* do nothing */}
try{ if (pStatement != null) pStatement.close(); } catch(Exception ex){/* do nothing */}
}
}
Run Code Online (Sandbox Code Playgroud)
我想知道,如果方法说它抛出异常,是否有一个像"最后"的地方我可以关闭方法的变量?例如:
public void anotherMethod() throws SQLException {
// This method doesn't need a try/catch because the method throws an exception.
InputStream inStream = null;
PreparedStatement pStatement = null; …Run Code Online (Sandbox Code Playgroud)