功能onclick不工作.. ...没有显示警报框

时间:2016-02-02 12:46:08

标签: javascript html

html代码

<input type="button" value="+" onclick="alert()" />

在检查它之后在chrome中显示此错误Uncaught RangeError:超出最大调用堆栈大小。

javscript

<script type="text/javascript">
  function alert() {
    alert("Hello! I am an alert box!!");
  }
</script>

通过告诉如何解决这个来帮助我

5 个答案:

答案 0 :(得分:1)

使用新定义覆盖现有函数定义,新定义另外调用自身,导致过多的递归。

答案 1 :(得分:0)

那是因为你在> bcrypt@0.8.5 install /home/travis/build/me/api/node_modules/bcrypt > node-gyp rebuild make: Entering directory `/home/travis/build/me/api/node_modules/bcrypt/build' CXX(target) Release/obj.target/bcrypt_lib/src/blowfish.o CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt.o CXX(target) Release/obj.target/bcrypt_lib/src/bcrypt_node.o In file included from /home/travis/.node-gyp/4.1.2/include/node/node.h:42:0, from ../node_modules/nan/nan.h:24, from ../src/bcrypt_node.cc:1: /home/travis/.node-gyp/4.1.2/include/node/v8.h:336:1: error: expected unqualified-id before ‘using’ /home/travis/.node-gyp/4.1.2/include/node/v8.h:469:1: error: expected unqualified-id before ‘using’ /home/travis/.node-gyp/4.1.2/include/node/v8.h:852:1: error: expected unqualified-id before ‘using’ In file included from ../node_modules/nan/nan.h:184:0, from ../src/bcrypt_node.cc:1: ../node_modules/nan/nan_maybe_43_inl.h:13:1: error: expected unqualified-id before ‘using’ ../node_modules/nan/nan_maybe_43_inl.h:16:1: error: expected unqualified-id before ‘using’ ../node_modules/nan/nan_maybe_43_inl.h:19:12: error: ‘Maybe’ does not name a type ../node_modules/nan/nan_maybe_43_inl.h:24:12: error: ‘Maybe’ does not name a type ../node_modules/nan/nan_maybe_43_inl.h:31:1: error: ‘MaybeLocal’ does In file included from ../node_modules/nan/nan_converters.h:59:0, from ../node_modules/nan/nan.h:189, from ../src/bcrypt_node.cc:1: ../node_modules/nan/nan_converters_43_inl.h:18:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Boolean>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:19:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Number>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:20:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::String>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:21:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Object>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:22:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Integer>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:23:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Uint32>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:24:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<v8::Int32>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:34:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<bool>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:35:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<double>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:36:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<long int>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:37:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<unsigned int>’ does not name a type ../node_modules/nan/nan_converters_43_inl.h:38:1: error: ‘return_t’ in ‘struct Nan::imp::ToFactory<int>’ does not name a type In file included from ../node_modules/nan/nan.h:190:0, from ../src/bcrypt_node.cc:1: ../node_modules/nan/nan_new.h: In function ‘v8::Local<T> Nan::imp::To(v8::Local<v8::Integer>) [with T = v8::Integer]’: ../node_modules/nan/nan_new.h:21:32: error: no matching function for call to ‘To(v8::Local<v8::Integer>&)’ ../node_modules/nan/nan_new.h:21:32: note: candidate is: ../node_modules/nan/nan_converters.h:53:38: note: template<class T> typename Nan::imp::ToFactory::return_t Nan::To(v8::Local<v8::Value>) ../node_modules/nan/nan_new.h: In function ‘v8::Local<T> Nan::imp::To(v8::Local<v8::Integer>) [with T = v8::Int32]’: ../node_modules/nan/nan_new.h:28:30: error: no matching function for call to ‘To(v8::Local<v8::Integer>&)’ ../node_modules/nan/nan_new.h:28:30: note: candidate is: ../node_modules/nan/nan_converters.h:53:38: note: template<class T> typename Nan::imp::ToFactory::return_t Nan::To(v8::Local<v8::Value>) ../node_modules/nan/nan_new.h: In function ‘v8::Local<T> Nan::imp::To(v8::Local<v8::Integer>) [with T = v8::Uint32]’: ../node_modules/nan/nan_new.h:35:31: error: no matching function for call to ‘To(v8::Local<v8::Integer>&)’ ../node_modules/nan/nan_new.h:35:31: note: candidate is: ../node_modules/nan/nan_converters.h:53:38: note: template<class T> typename Nan::imp::ToFactory::return_t Nan::To(v8::Local<v8::Value>) 中呼叫alert(),所以事实证明它是递归的。

答案 2 :(得分:0)

alert()是一个内置函数,不需要重新定义。虽然你重新定义,但你在新函数中调用相同的函数,这是错误的。因此,简单来说,给出一个不同的名称,如myAlert()或其他:

function myAlert() {
  alert("Hello! I am an alert box!!");
}

递归地调用alert()函数并使其溢出堆栈。

解决方案代码段

&#13;
&#13;
function myAlert() {
  alert("Hello! I am an alert box!!");
}
&#13;
<input type="button" value="+" onclick="myAlert()" />
&#13;
&#13;
&#13;

答案 3 :(得分:0)

你不能拥有一个递归函数,例如你在Javascript中所做的事情,因为它会继续失控并在浏览器中消耗更多内存。从函数中调用函数时,需要确保没有调用相同的函数。

答案 4 :(得分:0)

正如其他用户所提到的,你只需要使用与alert()不同的名称来创建你的函数,这是javascript中已经定义的函数,你不应该使用它。

我宁愿使用标准的addEventListener,也会使你的代码更干净,而且你的html

您可以执行以下操作,也可以直接在此页面尝试:

&#13;
&#13;
function myDefinedAlert() {
  alert("Hello! I am an alert box!!");
}

var el = document.getElementById("yourButton");
el.addEventListener("click", myDefinedAlert, false);
&#13;
<input id="yourButton" type="button" value="+" />
&#13;
&#13;
&#13;

按下按钮,试一试,然后将其复制到您的代码中,让我知道它是否有效:)