[英]Open an anchor link inside a div
I want to prevent an anchor
link to open on the document
body
and instead render inside a div
. 我想阻止在
document
body
上打开anchor
链接,而是在div
内部进行渲染。 I've used e.preventDefault()
but I think JQuery isn't targeting that event. 我使用过
e.preventDefault()
但我认为JQuery没有针对该事件。
I'm programmatic-ly creating a ul
and appending li
's to it from array
items then appending the ul
to a div
. 我是程序化地创建一个
ul
并从array
项追加li
然后将ul
附加到div
。 The structure looks like this: 结构如下所示:
//this is just a structure, not the actual HTML
<div id="results">
<ul> .ulStyle
<li> .list-group-item
-header
-p .lead
-a .anchorStyle
</li>
</ul>
</div>
Then, I add classes
to each element
. 然后,我为每个
element
添加classes
。 Everything but the ul
is getting the class
and I think that may be why it's not targeting the anchors
. 除了
ul
所有东西都在class
,我认为这可能就是为什么它不是针对anchors
。
jQuery : jQuery :
$(function(){
console.info('** anchor-tag click handler **')
/*
I've tried variations on the targeting from a
single $('.anchorStyle') to what you see below.
*/
$('div #results ul .ulStyle li .list-group-item a .anchorStyle')
.click(function(e) {
e.preventDefault();
$('#results').empty();
$('#results').load( this.getAttribute('href') );
console.log('anchor clicked...');
});
});
I get the first console
message on reload
but not the second on click
. 我在
reload
得到第一个console
消息,但on click
没有第二个
Questions: 问题:
Do I need a class on the ul
to target it properly? 我是否需要
ul
上的课程才能正确定位? I'm only adding it to the ul
for that purpose. 我只是为了这个目的将它添加到
ul
中。
Does this piece of code execute with everything else and the ul
may not have been created yet? 这段代码是否与其他所有代码一起执行,而
ul
可能尚未创建? JS/jQuery should still bind
and keep listening to an event
if it exists - or in this case, find the element
I'm targeting after the click event
fires, no? 如果
event
存在,JS / jQuery仍然应该bind
并继续监听event
- 或者在这种情况下,在click event
触发后找到我所针对的element
,不是吗?
Couple of problems in the code to point out... 在代码中指出几个问题......
First is that the selectors are not right. 首先是选择者不对。 You have a
space after your element and it's class
which is wrong (Eg: ul .ulStyle
here .ulStyle is considered as a child of ul).The selector must be like this 你
space after your element and it's class
后面有一个space after your element and it's class
是错的(例如: ul .ulStyle
这里.ulStyle被认为是ul的子节点。)选择器必须是这样的
$('div#results ul.ulStyle li.list-group-item a.anchorStyle')
Notice I have removed the spaces between the element and it's class.. 注意我删除了元素和它的类之间的空格。
Even after getting this changes done it still won't work for you because the elements are added dynamically. 即使在完成此更改后,它仍然不适合您,因为元素是动态添加的。 Taking this line from the OP
从OP获取此行
I'm programmatic-ly creating a ul and appending li's to it from array items then appending the ul to a div.
我是程序化地创建一个ul并从数组项追加li然后将ul附加到div。
So this calls for a need to use event delegation 因此,这需要使用事件委托
The syntax must change to 语法必须更改为
$(document).on('click','div#results ul.ulStyle li.list-group-item a.anchorStyle',function(){//your stuff here});
Further you can keep the selector simple as '#results a.anchorStyle'
此外,您可以将选择器保持为
'#results a.anchorStyle'
Here you have a working example with your structure: 这里有一个结构的工作示例:
$('#results ul li a').click(function(e){ e.preventDefault(); console.log(this.href); })
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="results"> <ul> <li> <header>HEADER</header> <p></p> <a href="/a">ANCHOR</a> </li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.