jQuery: Grabbing text() from clicked object
I know there are a lot of questions similar to this one already out there but none of them seemed to be the same as my current issue (if so sorry for the repeat).
What I am trying to create here is a script that will; upon clicking of a link, take the link name and type and find it in the folder. Then once it has been located add it into the DOM inside the specific container (#content in our case).
jQuery doesn't seem to want to grab text from an even.target (see code). I'm still learning jQuery so this may seem stupid and elementary to you so please be nice/patient.
CODE:
<!DOCTYPE HTML>
<html>
<head>
<met开发者_StackOverflowa http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Matt Elliott \\ Web Development \\ Video Production</title>
<link rel="stylesheet" href="main.css" media="screen" type="text/css">
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="jquery-ui-1.8.4.custom.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
//init vars
var adMenu = $('#adMenu');
var webMenu = $('#webMenu');
var videoMenu = $('#videoMenu');
var ad_btn = $('#adToggle');
var web_btn = $('#webToggle');
var video_btn = $('#videoToggle');
var nav = $('nav');
//init settings
adMenu.css({'opacity' : '0.0', 'visibility' : 'hidden'});
webMenu.css({'opacity' : '0.0', 'visibility' : 'hidden'});
videoMenu.css({'opacity' : '0.0', 'visibility' : 'hidden'});
//-------------------------------FUNCTIONS------------------------------\\
//event listeners
$('#main li, #prevContent, #nextContent').mouseover(function()
{
$(this).animate({'backgroundColor' : '#F90'}, 1000, 'easeOutElastic');
});
$('#main li, #prevContent, #nextContent').mouseleave(function()
{
$(this).animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
});
ad_btn.click(function()
{
if(videoMenu.css("opacity") == 1.0)
{
videoMenu.animate({opacity: 0.0}, 500);
video_btn.removeClass('selected');
}
else if(webMenu.css("opacity") == 1.0)
{
webMenu.animate({opacity: 0.0}, 500).removeClass('selected');
web_btn.removeClass('selected');
}
adMenu.css({'visibility' : 'visible'});
adMenu.animate({opacity: 1.0}, 2000);
ad_btn.addClass('selected');
ad_btn.unbind('mouseleave');
});
web_btn.click(function()
{
if(adMenu.css("opacity") == 1.0)
{
adMenu.animate({opacity: 0.0}, 500).removeClass('selected');
}
else if(videoMenu.css("opacity") == 1.0)
{
videoMenu.animate({opacity: 0.0}, 500).removeClass('selected');
}
webMenu.css({'visibility' : 'visible'});
webMenu.animate({opacity: 1.0}, 2000);
web_btn.addClass('selected');
});
video_btn.click(function()
{
if(adMenu.css("opacity") == 1.0)
{
adMenu.animate({opacity: 0.0}, 500).removeClass('selected');
}
else if(webMenu.css("opacity") == 1.0)
{
webMenu.animate({opacity: 0.0}, 500).removeClass('selected');
}
videoMenu.css({'visibility' : 'visible'});
videoMenu.animate({opacity: 1.0}, 2000);
video_btn.addClass('selected');
});
adMenu.click(function()
{
adMenu.animate({opacity: 0.0}, 200, function()
{
adMenu.css({'visibility' : 'hidden'});
ad_btn.removeClass('selected');
ad_btn.animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
ad_btn.mouseleave(function() {
$(this).animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
});
});
});
webMenu.click(function()
{
webMenu.animate({opacity: 0.0}, 200, function()
{
webMenu.css({'visibility' : 'hidden'});
web_btn.removeClass('selected');
web_btn.animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
web_btn.mouseleave(function() {
$(this).animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
});
})
});
videoMenu.click(function()
{
videoMenu.animate({opacity: 0.0}, 200, function()
{
videoMenu.css({'visibility' : 'hidden'});
video_btn.removeClass('selected');
video_btn.animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
video_btn.mouseleave(function() {
$(this).animate({'backgroundColor' : '#000'}, 300, 'easeInElastic');
});
});
});
});
function getThis(event,type)
{
string = $(event.target).text();
filename = string.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
if(type == 'jpg') {
$('#content').append('<img src="_work/ads/' + filename + '.jpg" />');
}
else if(type == 'swf') {
$('#content').append('<embed src="_work/ads/' + filename + '.swf" />');
}
}
</script>
<script type="text/javascript">
document.createElement('header');
document.createElement('nav');
document.createElement('article');
document.createElement('section');
document.createElement('footer');
document.createElement('canvas');
</script>
</head>
<body>
<header>
<h1>mattelliott.it</h1>
<nav>
<ul id="main">
<li id="adToggle"><h2>ads</h2></li>
<li id="webToggle"><h2>web</h2></li>
<li id="videoToggle"><h2>video</h2></li>
<li><h2>contact</h2></li>
</ul>
<!-- menu divs to fade in -->
<div id="adMenu">
<ul>
<li><a href="javascript:getThis('jpg');">bing</a></li>
<li><a href="#">emirates airlines</a></li>
<li><a href="#">fast forward</a></li>
<li><a href="#">prosche</a></li>
</ul>
</div>
<div id="webMenu">
<ul>
<li><a href="#">kashmere</a></li>
<li><a href="#">christopher stewart</a></li>
<li><a href="#">the gel lab</a></li>
<li><a href="#">erin foote</a></li>
<li><a href="#">cicis pizza</a></li>
<li><a href="#">pepsi natural</a></li>
<li><a href="#">totally rad</a></li>
</ul>
</div>
<div id="videoMenu">
<ul>
<li><a href="#">pepsi natural</a></li>
<li><a href="#">chasing numbers trailer</a></li>
<li><a href="#">gel lab shred session</a></li>
</ul>
</div>
</nav>
</header>
<div id="wrapper">
<section id="content">
<!-- <img src="_work/ads/bing.jpg" /> -->
<div id="contentControl">
<a id="prevContent" href="#"></a>
<a id="nextContent" href="#"></a>
</div>
</section>
</div>
I don't seem to get any errors though; which is weird. Thanks for all and any help.
when developing javascript, you may have to put "var " in front of variables you declare. it is a good idea, to use something different then the word "string" as variable, since it could cause a name conflict.
how about posting the proper HTML too so i can take a look?
Why don't you do something like this:
$(function(){
$('a').click(function(){
var link = $(this).text();
var filename = link.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
if(type == 'jpg') {
$('#content').append('<img src="_work/ads/' + filename + '.jpg" />');
}
else if(type == 'swf') {
$('#content').append('<embed src="_work/ads/' + filename + '.swf" />')
});
});
And drop the inline onClick handlers, which I assume you have set to run getThis().
I guess for now I will use the following to get this functioning. I'm running out of time for experimenting.
Thanks for all the help!
function getThis(name, type)
{
var filename = name.toLowerCase().replace(/ /g, '-').replace(/([^0-9a-z-])/g,'');
if(type == 'jpg') {
$('#content').append('<img src="_work/ads/' + filename + '.jpg" />');
}
else if(type == 'swf') {
$('#content').append('<embed src="_work/ads/' + filename + '.swf" style="z-index:4;" />');
}
}
Then to call it :
<li><a href="javascript:getThis('bing','jpg','1');">bing</a></li>
Thanks again. Will update if I come up with a more efficient way of handling this.
精彩评论