简体   繁体   English

使用 Prototype 或 Javascript 选择和隐藏元素

[英]Selecting and hiding elements with Prototype or Javascript

Anyone up for a Prototype challenge?有人准备挑战原型吗? I am new to Prototype and I am trying to do the following.我是 Prototype 的新手,我正在尝试执行以下操作。 Would do this in jQuery but the only js library installed is Prototype.会在 jQuery 中执行此操作,但唯一安装的 js 库是 Prototype。 Perhaps someone has a javascript solution that would work?也许有人有一个可行的 javascript 解决方案?

  1. Strip any whitespace out of the form value (before and after)从表单值中去除任何空格(之前和之后)
  2. If the input form length is 3 or less hide all the checkPrice.gif images in all rows.如果输入表单长度为 3 或更少,则隐藏所有行中的所有 checkPrice.gif 图像。

Not sure if this is possible using Prototype.不确定这是否可以使用原型。

<form method="get" id="searchForm" name="car" action="some-action">
<input type="text" value="Ford150" name="carPart" id="search" class="textContent">
</form>

<table border="1">
<tr>
<td class="description">Description:</td>
<td class="checkPrice"><p>Type:</p>
<p><a target="_blank" href="link.html"><img src="images/checkPrice.gif"></a></p>
</td>
</tr>

<tr>
<td class="description">Description:</td>
<td class="checkPrice"><p>Type:</p>
<p><a target="_blank" href="link.html"><img src="images/checkPrice.gif"></a></p>
</td>
</tr>
</table>
  • Rows repeat行重复

Thanks so much to anyone who can help!非常感谢任何可以提供帮助的人!

I assume you mean "If the input form value length is 3 or less" since the default input value is not a number.我假设您的意思是“如果输入表单值长度为 3 或更少”,因为默认输入值不是数字。

function updateCheckPrice(event) {
    // $F() returns a string
    // String.strip() trims whitespace and returns a new string
    // String.length is a native property
    var length = $F(this).strip().length;

    // If length is 3 or less...
    var action = length <= 3 ? Element.hide : Element.show;

    // Pass the chosen show/hide function to every img element
    $$('img[src$=checkPrice.gif]').each(action);
}

document.observe('dom:loaded', function(){
    // Update as you type
    Event.observe('search', 'keyup', updateCheckPrice);
});

Ok.好的。 Trim-function written in pure JS (from http://blog.stevenlevithan.com/archives/faster-trim-javascript ):用纯 JS 编写的修剪函数(来自http://blog.stevenlevithan.com/archives/faster-trim-javascript ):

function trim (str) {
var str = str.replace(/^\s\s*/, ''),
    ws = /\s/,
    i = str.length;
while (ws.test(str.charAt(--i)));
return str.slice(0, i + 1);
}

How to convert string to number: http://www.javascripter.net/faq/convert2.htm如何将字符串转换为数字: http://www.javascripter.net/faq/convert2.htm

How to access element's value using Prototype: http://www.prototypejs.org/api/form/element/getValue如何使用原型访问元素的值: http://www.prototypejs.org/api/form/element/getValue

How to check is variable - string:如何检查变量 - 字符串:

 function is_string(input){
    return typeof(input)=='string';
 }

How to hide element using Prototype: http://www.prototypejs.org/api/element/hide如何使用原型隐藏元素: http://www.prototypejs.org/api/element/hide

Now you are ready to solve the task by yourself;现在您可以自己解决任务了; ;) ;)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM