我需要一个帮助。我在 onclick 函数中传递 this
关键字。如果用户需要在不使用 onclick 事件的情况下调用相同的函数,将如何使用 javascript 调用它。我正在解释下面的场景。
<input name="optional_0_0_ans" id="optional_0_0_ans" class="form-control firstsec" placeholder="Answer" value="" type="text"> <button type="button" class="btn btn-sm btn-success" style="line-height:12px;" onclick="createNew(this,0,0,1);"><i class="fa fa-plus" aria-hidden="true"></i></button>
<script>
function createNew(evt,index,childindex,hidevalue){
}
</script>
在上面的代码中,我在 onclick 事件中传递了 this
关键字。这里假设用户想要在其他地方调用 createNew
函数并希望传递相同的内容这个
关键字那么它将如何完成。请帮助我。
最佳答案
Here suppose user wants to call the
createNew
function some where else and wants to pass the samethis
key word then how it will be done.
这是不可能的,因为 this
仅存在于事件处理程序内部。 “相同的this
”在处理程序之外不存在。
我猜你的意思是他们想传递相同或相似的值。那么 this
的值是多少?它是一个 DOM 元素。他们只需要获取 DOM 元素的引用并传递它:
var someElement = // get reference to a DOM element
createNew(someElement,0,0,1);
关于javascript - 如何在javascript中的onclick函数之外传递这个关键字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38091578/