简体   繁体   English

从锚标记获取元素ID并替换href

[英]Getting element id from anchor tags and replace href

I have a Joomla project, and I have slight problem changing the links in the dropdown menu list. 我有一个Joomla项目,并且在更改下拉菜单列表中的链接时遇到了小问题。

I have this menu and its child menu. 我有此菜单及其子菜单。

Shop
- firstcat -第一猫
- secondcat -第二只猫
- thirdcat -第三只猫

HTML: 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>`

Default in Joomla that the menu items are linked to their category_alias. 在Joomla中,默认情况下,菜单项链接到其category_alias。 But I want to link the menus to their category_id. 但是我想将菜单链接到它们的category_id。 Now, I want to get their different id(s) and find their corresponding category_ids in the database and replace the href in to their category_id. 现在,我想获取它们的不同ID,并在数据库中找到它们对应的category_id,并将href替换为他们的category_id。

Like this: 像这样:

<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>

I just wanna do it through JavaScript or jQuery, because it takes time to dig files in Joomla and replace the alias into id. 我只想通过JavaScript或jQuery进行操作,因为在Joomla中挖掘文件并将别名替换为id需要花费时间。

For now, I only do it manually. 目前,我仅手动执行此操作。
$('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');

In php part that I have to get their category_id is this. 在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 -> gets the alias of the current item that is being viewed. $menuItem->alias获取正在查看的当前项目的别名。

add this jQuery script.... 添加此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");

Assuming you have an array of {category_id:"someValue", category_alias:"someValue"} myArr in your JS (maybe you can get this with an ajax call, however you do it) 假设你有一个array{category_id:"someValue", category_alias:"someValue"} myArr在你的JS(也许你可以使用Ajax调用得到这个,但是你这样做)

do something like 做类似的事情

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);
}

I did not have time to test this though so please feel free to correct me if I'm wrong. 我没有时间进行测试,所以如果我错了,请随时纠正我。

wew! 哇! Sorry for the disturbance guys.. But I have to do ajax call to make it work. 很抱歉给我打扰。.但是我必须做ajax调用才能正常工作。

I do it like this. 我是这样的

My PHP: 我的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();
}

My Script: 我的剧本:
<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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM