从锚标签获取元素id并替换href

时间:2013-10-21 08:05:23

标签: javascript jquery html mysql joomla

我有一个Joomla项目,我在下拉菜单列表中更改链接时遇到了一些问题。

我有这个菜单及其子菜单。

商店
- 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 - &gt;获取正在查看的当前项目的别名。

3 个答案:

答案 0 :(得分:0)

添加此jQuery脚本....

$("#firstcat").attr("href","/my_website/index.php/shop?16");
$("#secondcat").attr("href","/my_website/index.php/shop?17");
$("#thirdcat").attr("href","/my_website/index.php/shop?18");

答案 1 :(得分:0)

假设你的JS中有array {category_id:"someValue", category_alias:"someValue"} myArr(也许你可以通过ajax调用得到它,但是你这样做了)

做类似

的事情
var length = myArr.length;
for(var i = 0 ; i < length ; i++) {
    $("#" + myArr[i].category_alias).attr("href","/my_website/index.php/shop?" + myArr[i].category_id);
}

我没时间测试这个,所以如果我错了,请随时纠正我。

答案 2 :(得分:0)

WEW!对不起干扰的家伙..但我必须做ajax调用才能使它工作。

我是这样做的。

我的PHP:
$q = $_GET['q'];
$db = & JFactory::getDBO();
$db->setQuery('SELECT id FROM #__k2_categories WHERE alias="' . $q . '"');
$result = $db->loadResult();
if (!empty($result)) {
echo $result;
exit();
}

我的剧本:
<script type="text/javascript">
            (function($){
                $(document).ready(function() {
                    $('.level2').click(function(e) {
                        CurrElId = jQuery(this).attr("id");
                        $.ajax({
                            type: 'GET',
                            url: location.href,
                            data: 'q='+CurrElId,
                            success: function(data) {
                                var basepath = "/mywebsite/index.php/shop";
                                location.href = basepath+'?'+data;
                            }
                        });
                        e.preventDefault();
                    });
                });
            })(jQuery);
        </script>

相关问题