我有一個Joomla項目,並且我在更改下拉菜單列表中的鏈接時遇到了輕微的問題。從定位標記獲取元素ID並替換href
我有這個菜單及其子菜單。
店
- firstcat
- secondcat
- thirdcat
HTML:
<ul class="level2">
<li>
<a id="firstcat" href="/my_website/index.php/shop/firstcat">First Cat</a>
</li>
<li>
<a id="secondcat" href="/my_website/index.php/shop/secondcat">Second Cat</a>
</li>
<li>
<a id="thirdcat" href="/my_website/index.php/shop/thirdcat">Third Cat</a>
</li>
</ul>`
默認的Joomla中的菜單項與他們category_alias。但我想將菜單鏈接到他們的category_id。現在,我想要獲取其不同的ID並在數據庫中找到相應的category_ids,並將href
替換爲其category_id。
像這樣:
<a id="firstcat" href="/my_website/index.php/shop?16">FirstCat</a>
<a id="secondcat" href="/my_website/index.php/shop?17">SecondCat</a>
<a id="thirdcat" href="/my_website/index.php/shop?18">ThirdCat</a>
我只是想做到這一點通過JavaScript或jQuery的,因爲它需要時間的Joomla挖掘文件,並替換成別名ID。
現在,我只做手動。
$('ul.level2 li a#firstcat').attr('href','/my_website/index.php/shop?catid=16');
$('ul.level2 li a#secondcat').attr('href','/my_website/index.php/shop?catid=17');
$('ul.level2 li a#thirdcat').attr('href','/my_website/index.php/shop?catid=18');
在PHP的一部分,我必須讓他們CATEGORY_ID是這樣的。
<?php
echo $menuItem->alias . '<br/>';
$db = & JFactory::getDBO();
$db->setQuery('SELECT id FROM #__k2_categories WHERE alias="' . $menuItem->alias . '"');
$result = $db->loadResult();
echo $result;
foreach ($result as $res) {
echo $res->id;
}
?>
$menuItem->alias
- >獲取當前正在查看的項目的別名。
喜@ f00bar,我已經編輯我的職務。請檢查。 – user2767055
好吧,那麼你必須編寫一些Ajax調用目標這個PHP腳本...然後這個服務器端腳本應該返回一個JSON對象包含所有鏈接的數組與他們的別名,所以你可以取代菜單中的原始鏈接。 )' – Stphane
是的..我做了一些阿賈克斯電話..謝謝.. – user2767055