javascript - 如何在javascript中的onclick函数之外传递这个关键字

标签 javascript jquery

我需要一个帮助。我在 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 same this 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/

相关文章:

javascript - 禁用所有 ID 属性以

validation - Backbonejs集合.create : how to prevent it from adding items if there is an error?

javascript - 如何在发布数据中进行ajax调用 --> 图像和自定义数据(名称和值对)

jquery - 是否可以使用 jquery 对表进行分页?

javascript - Vue.js 标记为无效 HTML

javascript - 如何使用 javascript 循环在特定的 div 中显示 * 模式

php - ActiveDropDownList 中的默认值

javascript - jquery 中的 $.inArray 无法正常工作

javascript - lodash 链式方法中的当前链

javascript - 将数据从 HTML 表单获取到 Microsoft Flow 的方法,无需页面刷新且无需 php