简体   繁体   English

我可以在javascript中使用函数参数的不同方法是什么

[英]What are different ways i can use function arguments in javascript

function hide (x, reflow2) {
  if (reflow2) { 
    x.style.display = "none";
  } 
  else {
    x.style.visibility = "hidden";
  }
}

function debug(msg) {
  //find the debugging section of the document, looking at html id attributes
  var log = document.getElementById("debuglog");

  //if no element with the id "debuglog" exists, create one.

  if (!log) {
    log = document.createElement("div");
    log.id = "debuglog"; 
    log.innerHTML = "<h1> Debug log </h1>";
    document.body.appendChild(log);
  }

  //now wrap the message in its own <pre> and append it to the log

  var pre = document.createElement('pre');
  var text = document.createTextNode(msg);
  pre.appendChild(text);
  log.appendChild(pre);
}

function highlight(e) { 
  var divi = document.getElementByTagName('p');

  if (!e.className)
    e.className="hilite";
 }
 else {
   e.className += "hilite";
  }

the html: HTML:

<p>Whatever</p>
<button onclick="hide(this,true); debug('hide button 1'); highlight(p);">hide    button</button>

In the example above, i'm having trouble getting the highlight function to highlight the paragraph element. 在上面的示例中,我在获取高亮功能以突出显示段落元素时遇到了麻烦。 can someone please show me what i'm doing wrong? 有人可以告诉我我在做什么错吗?

I would also like to know how i can use functions other than functionname(this); 我也想知道我如何使用除functionname(this)之外的函数。 functionname(textmessage); functionname(textmessage);

what other ways can i pass information like elements into a function? 我还可以通过哪些其他方式将元素等信息传递给函数?

In your buttons callback, you are calling highlight(p) . 在按钮回调中,您正在调用highlight(p) In this context p is an undefined variable, thus nothing gets highlighted. 在这种情况下, p是未定义的变量,因此不会突出显示任何内容。 I suggest you give your <p> element an ID and modify the callback to retrieve the <p> element by its ID: 我建议您给<p>元素提供一个ID,并修改回调以通过其ID检索<p>元素:

<p id="myP">Whatever</p>

<button onclick="hide(this, true);
                 debug('hide button 1');
                 highlight(document.getElementById('myP'));">Hide button</button>

Of course, it would be even better to move all that callback code in a function of its own and register it as a callback for the button's click event: 当然,最好将所有回调代码移至其自身的函数中,并将其注册为按钮的click事件的回调,这将更好:

/* In HTML: */
<p id="myP">Whatever</p>
<button id="hideBtn">Hide button</button>

/*  In JS */
window addEventListener("DOMContentLoaded", function() {
    var myP = document.getElementById("myP");
    var btn = document.getElementById("hideBtn");
    btn.addEventListener("click", function() {
        hide(btn, true);
        debug('hide button 1');
        highlight(myP);
    });
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我如何使用切换和 function 从前一个元素中删除 class 仅使用 JavaScript 我尝试过不同的方式? - how i can use toggle and function that remove the class from the previous element together only using JavaScript i have tried to Different ways? 这两种在JavaScript函数中利用arguments对象的方式有什么区别? - What is the difference between these two ways to leverage the arguments object in a JavaScript function? 在 JavaScript 中可以将参数传递给函数的所有方法是什么? - What are all the ways you can pass arguments into functions in JavaScript? 我可以通过哪些方式使用 $? - What are all the ways I can use $? HTML javascript 表格 - 我还可以通过什么其他方式编码此 javascript ZC1C425268E68385D1AB5074C17A94F14 表格 - HTML javascript form - what other ways can I code this javascript function with form validation 在javascript对象中创建函数的不同方法有什么区别? - What is the difference between different ways to create a function inside javascript object? 将 SQLite (在服务器上)与纯 JavaScript (在浏览器中)一起使用有哪些不同的方法? - What are different ways to use SQLite (on the server) with pure JavaScript (in the browser)? JavaScript以不同的方式调用函数 - JavaScript invoking a function in different ways 以不同方式调用javascript函数 - Calling a javascript function in different ways Javascript - 概括一个可以在相同上下文中以两种不同方式使用的函数 - Javascript - Generalize a function that can be used in two different ways but in the same context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM