CakePHP,jQuery的 - 添加数据属性锚
我正在寻找一种方法来插入数据属性到我的表锚在CakePHP。它会自动生成以下代码,但我不知道如何以使其成为数据目标属性的方式进行修改。CakePHP,jQuery的 - 添加数据属性锚
我想要实现这一目标的原因是因为我链接到同一页上的不同div,并且想要阻止页面重新加载。我需要从jQuery插入preventDefault()
吗?如果是这样,该怎么做?
这是我当前的代码:
<?php foreach ($servers as $server): ?>
<tr>
<td><?= $this->Number->format($server->id) ?></td>
<td><?= h($server->url) ?></td>
<td><?= h($server->description) ?></td>
<td><?= h($server->Timestamp) ?></td>
<td class="actions">
<?= $this->Html->link('View', array('#' => 'admin-view-' . $server->id)) ?>
<?= $this->Html->link('Edit', array('#' => 'admin-edit-' . $server->id)) ?>
</td>
</tr>
<?php endforeach; ?>
下面是我在寻找的结果;它使用CakePHP的约定(不介意列表项)来完成:
<a data-target="admin-edit">Edit</a>
<a data-target="admin-view">View</a>
你有没有试过这样: -
<?= $this->Html->link('View',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-view");?>
<?= $this->Html->link('Edit',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-edit");?>
从页面重载预防,创建href="javascript:void(0);"
链接如下图所示: -
<?= $this->Html->link('View','javascript:void(0);',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-view");?>
<?= $this->Html->link('Edit','javascript:void(0);',array('#' => 'admin-view-' . $server->id,'data-target'=>"admin-edit");?>
如果你想防止通过jQuery的(不与上面的代码),则: -
$('link').click(function(e){
e.preventDefault();
//rest your code
});
注意: - 确保在此代码之前添加了jQuery库,并且此代码必须位于页面的底部。
这里是关于CakePHP的的HtmlHelperlink
方法
的CakePHPlink
方法接受3个参数作为其参数的细节。
- 标题 [字符串]
- 地址 [字符串或数组]
- 选项/属性 [阵列]
语法:
$this->Html->link($title, $url = null, array $options = []);
例子:
echo $this->Html->link(
'Title',
'/path/to/url', #OR ['controller'=>'','action'=>'','others']
[
'class' => 'button',
'target' => '_blank',
'data-url'=>'#',
'data-path'=>'',
/*Other attributes*/
]
);
这里是关于Creating Links in CakePHP
有关问题的详细信息,如果没有必要
href=''
在<a>
标签,然后你可以使用button
或其他标记,而不是<a>
标签。
的详细信息+1 –
谢谢! :) +1也@AlivetoDie –
谢谢!由于它仍然是一个锚点,你知道如何防止页面重新加载,一旦你点击锚点(最好使用jQuery)? – Levano
@Levano是检查答案请。很高兴帮助你:) –