这是我的代码:

<script language="javascript" type="text/javascript"> $(document).ready(function(){ $('.tree div').click(function(){ var id = $(this).attr('id'); $('.tree div:regex(id,^['+id+'_])').html('this one'); }); });</script><div class="tree"> <div id="item_1"> <a href="<?php echo $this->url(array('action' => 'ketab')); ?>"><?php echo $this->translate("Add Ketab"); ?></a> </div> <div id="item_1_1"> <span></span> <a href="<?php echo $this->url(array('action' => 'ketab')); ?>"><?php echo $this->translate("Add Ketab"); ?></a> </div> <div id="item_1_2"> <span></span> <a href="<?php echo $this->url(array('action' => 'ketab')); ?>"><?php echo $this->translate("Add Ketab"); ?></a> </div></div>



当我单击item_1_1时,我想隐藏item_1_2item_1,我不知道如何使用正则表达式来实现。

我将http://james.padolsey.com/javascript/regex-selector-for-jquery/用于regx

最佳答案

这里不需要正则表达式选择器。使用jQuery内置的attribute-starts-with selector

$('#item_1').click(function () {
    $('[id^="item_1"]').hide();
});


如果您仍然想使用该插件,那么您的选择器将是:

':regex(id, ^item_1)'

10-01 01:30
查看更多