将 Jquery 函数转换为纯 Javascript

发布于 2024-12-03 16:14:46 字数 858 浏览 1 评论 0原文

这是我关于这个主题的第三个问题。由于我不会详细说明的原因,我无法在我正在工作的网站上使用 jquery。您建议我如何将这段代码翻译成纯 Javascript:

    <script> 
    $(document).ready(function(){
        $('#rule-type').change(function() {
           var val = $(this).val();
           if (val == 'tid and acc') {
              $('#tid-acc').show();
           }
           else {
              $('#tid-acc').hide(); 
           }
        });
    });
</script>

<select id="rule-type">
    <option value="" selected="selected">None</option>
    <option value="tid">tid</option>
    <option value="tid and acc">tid and acc</option>
    <option value="xid">xid</option>
</select>
<input id="tid-acc">

提前致谢!这是 jfiddle 的链接: http://jsfiddle.net/Wx8Jf/2/

This is my third question on the subject. For reasons I won't go into, I cannot use jquery on the site I am working on. How would you suggest I translate this block of code into pure Javascript:

    <script> 
    $(document).ready(function(){
        $('#rule-type').change(function() {
           var val = $(this).val();
           if (val == 'tid and acc') {
              $('#tid-acc').show();
           }
           else {
              $('#tid-acc').hide(); 
           }
        });
    });
</script>

<select id="rule-type">
    <option value="" selected="selected">None</option>
    <option value="tid">tid</option>
    <option value="tid and acc">tid and acc</option>
    <option value="xid">xid</option>
</select>
<input id="tid-acc">

Thanks in advance! Here is a link to jfiddle: http://jsfiddle.net/Wx8Jf/2/

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

愿得七秒忆 2024-12-10 16:14:46
window.onload = function(){ //when the window has loaded

    //store the input in a variable
    var input = document.getElementById('tid-acc');

    //when the select changes 
    document.getElementById('rule-type').onchange = function() { 
       var val = this.value; 
       if (val == 'tid and acc') {
           input.style.display = ''; //show
       }
       else {
           input.style.display = 'none'; //hide
       }
    };
}

小提琴: http://jsfiddle.net/Wx8Jf/12/

window.onload = function(){ //when the window has loaded

    //store the input in a variable
    var input = document.getElementById('tid-acc');

    //when the select changes 
    document.getElementById('rule-type').onchange = function() { 
       var val = this.value; 
       if (val == 'tid and acc') {
           input.style.display = ''; //show
       }
       else {
           input.style.display = 'none'; //hide
       }
    };
}

Fiddle: http://jsfiddle.net/Wx8Jf/12/

泛滥成性 2024-12-10 16:14:46

这将是相当艰难的——我认为最好的答案就是做出牺牲。

$('#tid-acc').show();

例如是一个动画,所以你可能最好在元素上操作 CSS 来显示/隐藏。

$('#rule-type').change()

根据文档,只是将事件处理程序绑定到 javascript 更改事件,因此您可以查找并尝试替换。

document.ready() 也可能被 window.onload 替换

我相信我已经给了你一些指示 - 但我不准备简单地为你做这项工作。

This will be quite tough - and I think the best answer would be to make sacrifices.

$('#tid-acc').show();

for example is an animation, so you'd probably be better of manipulating the CSS on the element to show/hide.

$('#rule-type').change()

According to the docs just binds and event handler to the javascript change event, so you could look this up and try substituting in.

document.ready() might also be substituted somewhat by window.onload.

I believe I've given you a few pointers there - but I am not prepared to simply do the work for you.

别再吹冷风 2024-12-10 16:14:46

为了防止对象从内容流中删除,我将使用 visibility 而不是 display...

http://jsfiddle.net/Wx8Jf/13/

window.onload = function(){
 var element = document.getElementById('tid-acc');
 document.getElementById('rule-type').onchange = function() {
   var val = this.value;
   if (val == 'tid and acc') {
       element.style.visibility = 'visible';
   }
   else {
       element.style.visibility = 'hidden';
   }
 };
}

To prevent the object from being removed from the content flow, I'd use visibility instead of display...

http://jsfiddle.net/Wx8Jf/13/

window.onload = function(){
 var element = document.getElementById('tid-acc');
 document.getElementById('rule-type').onchange = function() {
   var val = this.value;
   if (val == 'tid and acc') {
       element.style.visibility = 'visible';
   }
   else {
       element.style.visibility = 'hidden';
   }
 };
}
怀中猫帐中妖 2024-12-10 16:14:46

您已经得到了一些很好的答案,这里有一些 onload 的替代方案。

如果您将脚本放在其所应用的元素之后,则可以在文档准备好后且在加载事件触发之前运行该脚本。最简单的方法是将其放在结束 body 标记之前。

元素位于文档中后立即添加侦听器的另一个选项是内联处理程序,例如

<script type="text/javascript">

// Library functions that are reused  
function showElement(id) {
  var element = typeof id == 'string'? document.getElementById(id) : id;
  element.style.display = '';
}

function hideElement(id) {
  var element = typeof id == 'string'? document.getElementById(id) : id;
  element.style.display = 'none';
}

// Ad hoc function for this page
function ruleTypeChange(el, id) {
  el.value == 'tid and acc'?  showElement(id) : hideElement(id);
}

</script>

<!-- inline handler -->
<select id="rule-type" onchange="ruleTypeChange(this, 'tid-acc')">
  ...
</select>
<input id="tid-acc">


<!-- alternative - use a bottom script -->
<script type="text/javascript">
document.getElementById('rule-type').onchange = function() {
  ruleTypeChange(this, 'tid-acc');
};
</script>

“底部脚本”可以位于元素之后的任何位置,但通常所有“onload”函数都在关闭之前放入单个脚本中身体标签。这也使得文档加载速度更快,并且是一种简单的实施方法。

关于内联处理程序,您经常会听到那些使用复杂选择器查找元素的人声称“脚本应该与代码分开”,但一旦文档结构发生更改,它们就会中断。添加内联侦听器并不比添加稍后可能用于添加侦听器的类或维护依赖于文档结构的选择器更麻烦。它们可以通过与用于添加类、id 或数据属性类似或相同的服务器逻辑来添加。

无论如何,做对你最有利的事情,只要记住质疑教条并找出为什么应该以特定方式完成某些事情背后的原因。当您了解这一点时,您就可以做出自己明智的选择。

You've got some good answers, here are some alternatives to onload.

You can have the script run as soon as the document is ready and before the load event fires if you put the script after the element it applies to. The easiest way is to put it before the closing body tag.

Another option that adds the listener as soon as the element is in the document is an inline handler, e.g.

<script type="text/javascript">

// Library functions that are reused  
function showElement(id) {
  var element = typeof id == 'string'? document.getElementById(id) : id;
  element.style.display = '';
}

function hideElement(id) {
  var element = typeof id == 'string'? document.getElementById(id) : id;
  element.style.display = 'none';
}

// Ad hoc function for this page
function ruleTypeChange(el, id) {
  el.value == 'tid and acc'?  showElement(id) : hideElement(id);
}

</script>

<!-- inline handler -->
<select id="rule-type" onchange="ruleTypeChange(this, 'tid-acc')">
  ...
</select>
<input id="tid-acc">


<!-- alternative - use a bottom script -->
<script type="text/javascript">
document.getElementById('rule-type').onchange = function() {
  ruleTypeChange(this, 'tid-acc');
};
</script>

The "bottom script" can be anywhere after the element, but usually all "onload" functions are put in a single script just before the closing body tag. This also gives the appearance of a faster loading document and is a simple methodology to implement.

In regard to inline handlers, you will often hear claims that "script should be separate from code" from those who use complex selectors to find elements, only to have them break as soon as the document structure is changed. Adding inline listeners is no more of a maintenance headache than adding a class that might be used to add a listener later, or maintain selectors that are dependent on document structure. They can be added by similar or identical server logic as would be used to add a class, id or data- attribute.

Anyhow, do what is best for you, just remember to question dogma and get the reason behind why something should be done a particular way. When you understand that, you can make your own informed choices.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文