小编sim*_*ung的帖子

父/兄弟/子上的jQuery removeClass

我使用以下jQuery来更改无序列表中的元素类.它似乎不是实现这种效果的最有效方式.有没有更好的方法来写这个?

$(function() {
    $('nav li a').click( function() {
        $(this).parent().siblings().children().removeClass('active');
        $(this).addClass('active');
    });
});
Run Code Online (Sandbox Code Playgroud)

谢谢

小号

jquery

20
推荐指数
2
解决办法
3万
查看次数

使用jQuery/javascript循环一个字符串

我使用jQuery从当前网页加载一个简单的txt文件 - 这个文件将始终包含一个没有格式的普通数字 - 例如123456

$(document).ready(function(){    

  var test;
  $.getJSON('myfile.txt', function(data) {
    test = data;
    showAlert(); // this call will display actual value
  });

  function showAlert() {
    alert(test);
  }

});
Run Code Online (Sandbox Code Playgroud)

此时,代码将文件拉入,然后在警告框中显示内容,但我想要做的是逐个字符地读取响应字符并创建一个HTML字符串,然后我可以将其插入到页面中 - 每个字符将被转换为图像标签.

例如,如果响应为123,我想创建一个包含以下HTML的字符串:

<img src="1.png" />
<img src="2.png" />
<img src="3.png" />
Run Code Online (Sandbox Code Playgroud)

然后我会将该字符串插入到我页面上的div中.

任何人都可以建议如何循环创建img标签的响应?

谢谢

javascript jquery

18
推荐指数
4
解决办法
5万
查看次数

在Magento中,如何使用sales_flat_order中的数据填充sales_order_grid中的新列?

我正在关注Ivan的教程(将订单属性添加到Magento 1.4.1中的订单网格)以向sales_order_grid表(Shipping Description文本)添加一个额外的列,并且它正在工作,除了它不会将sales_flat_order中的旧数据带到新列在sales_order_grid中.

我的SQL安装脚本正确地添加了列,因为我使用与sales_flat_order中相同的字段名称我认为我不需要观察者,但是将所有现有出货描述数据导入到shipping_description字段的代码不是运行.

我做错了什么?

我的SQL安装脚本:

<?php
/**
 * Setup scripts, add new column and fulfills
 * its values to existing rows
 *
 */
/* @var $this Mage_Sales_Model_Mysql4_Setup */
$this->startSetup();

// Add column to grid table
$this->getConnection()->addColumn(
    $this->getTable('sales/order_grid'),
    'shipping_description',
    "varchar(255) not null default ''"
);

// Add key to table for this field,
// it will improve the speed of searching & sorting by the field
$this->getConnection()->addKey(
    $this->getTable('sales/order_grid'),
    'shipping_description',
    'shipping_description'
);


// fill existing rows with data
$select …
Run Code Online (Sandbox Code Playgroud)

magento

5
推荐指数
1
解决办法
5159
查看次数

扩展Magento OnePage结帐课程

我正在通过一个新模块向Magento结账添加一个额外的复选框字段以获取新闻通讯订阅.

到目前为止,我已将代码添加到我的布局文件(billing.phtml):

<p>Please untick this box if you do not wish to receive infrequent email updates and newsletters from us. <input type="checkbox" name="billing[is_subscribed]" title="" value="1" id="billing:is_subscribed" class="checkbox" checked="checked" /></p>
Run Code Online (Sandbox Code Playgroud)

我扩展了类(app/code/local/Clientname/Checkout/Model/Type/Onepage.php) - 我只扩展了savebilling方法:

<?php
/**
 * Magento
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the Open Software License (OSL 3.0)
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * …
Run Code Online (Sandbox Code Playgroud)

magento

4
推荐指数
1
解决办法
8851
查看次数

用于计算百分位数的Ruby on Rails方法 - 可以重构吗?

我已经编写了一种方法来计算一组数字的给定百分位数,以便在我正在构建的应用程序中使用.通常,用户需要知道给定数字集和第75百分位数的第25百分位数.

我的方法如下:

def calculate_percentile(array,percentile)
 #get number of items in array
 return nil if array.empty?

 #sort the array
 array.sort!

 #get the array length
 arr_length = array.length

 #multiply items in the array by the required percentile (e.g. 0.75 for 75th percentile)
 #round the result up to the next whole number
 #then subtract one to get the array item we need to return
 arr_item = ((array.length * percentile).ceil)-1

 #return the matching number from the array
 return array[arr_item]

end
Run Code Online (Sandbox Code Playgroud)

这看起来提供了我期待的结果,但任何人都可以重构这个或提供一个改进的方法来返回一组数字的特定百分位数?

ruby ruby-on-rails

2
推荐指数
1
解决办法
3279
查看次数

jQuery div autoscroll

我正在寻找有关如何使用jQuery创建自动滚动效果的建议,这将使页面中的整个div在以恒定的低速加载时开始垂直滚动.这将是一个具有大量内容的div,其中任何时候屏幕上只能看到少量内容.

滚动需要是自动的,平滑的并且以定义的速率,例如每秒10个像素.另外,当滚动到达页面底部时,我需要能够调用一个函数.

我已经尝试了一些不同的jQuery插件,但发现什么都没有可靠的工作.任何人都可以建议采取措施吗?

谢谢

西蒙

javascript jquery

2
推荐指数
1
解决办法
2万
查看次数

如何更有效地重写这个jQuery?

如何重写此代码以更有效地使用jQuery更改类名?

$(function() {
    $('#container').click(function(event) {
      var element = event.currentTarget;
      /* Toggle the setting of the classname attribute */
      element.className = (element.className == 'card') ? 'card flipped' : 'card';
    });
});
Run Code Online (Sandbox Code Playgroud)

jquery

2
推荐指数
1
解决办法
70
查看次数

添加到acts_as_taggable_on的连字符标签

我在我的应用程序中使用acts_ as_ taggable_on并使其工作正常,但我正在寻找有关如何进行一次修改的信息.

目前,如果我输入包含空格的标签,标签将与这些空格一起保存,因此要查看具有此标签的所有记录,我有类似的内容:

http://myapp.local/tag/this%20tag%20has%20spaces

当act_ as_ taggable_on首次保存标签时,如何将标签连字符,以便标签存储为this-tag-has-spaces?

我可以如下替换值,但是如何在acts_ as_ taggable_on接管并保存标记列表之前执行此操作?

tag.downcase.gsub(/[^[:alnum:]]/,'-').gsub(/-{2,}/,'-')

谢谢

西蒙

ruby-on-rails acts-as-taggable

1
推荐指数
1
解决办法
602
查看次数

原型重写jQuery函数

我是一个jQuery猴子,我有一个网站,我需要使用一个简单的功能在一个单独的窗口中打开与rel ="external"的链接.

在jQuery中我这样做:

// make links with rel=external open in new window/tab
$(function() {
    $('a[rel*=external]').click( function() {
        window.open(this.href);
        return false;
    });
});
Run Code Online (Sandbox Code Playgroud)

我如何在原型中设置相同的功能,以便在任何连接到rel = external的链接上不引人注意地触发它?

谢谢

西蒙

编辑:

根据以下所有三个答复的建议,最终代码为:

    $(document).on('dom:loaded', function() {
    $$('a[rel*=external]').invoke('on','click', function(event) {
        window.open(this.href);
        event.stop();
    });
});
Run Code Online (Sandbox Code Playgroud)

谢谢大家!

javascript jquery prototypejs

1
推荐指数
1
解决办法
889
查看次数