简体   繁体   English

尝试在我认为是变量的范围内创建Javascript链接?

[英]Trying to create a Javascript link within what I believe are variables?

I downloaded a theme and seem to be having some trouble. 我下载了一个主题,似乎遇到了一些麻烦。 The page in question is here - http://pureearthsoaps.com/test.html 有问题的页面在这里-http://pureearthsoaps.com/test.html

Each product is created using (if I've been explained correctly) Javascript variables. 每个产品都是使用(如果我已经正确解释的话)Javascript变量创建的。 See below: 见下文:

  1: {
name: 'Bar Soap',
scent: 'Cucumber & Oatmeal, Patchouli, Peppermint, Frankincense, Holiday, Coffee & Hazelnut, Apple, and more',
size: '1 bar',
price: '3.<span class="small">95</span>',
short_desc: 'An innovative and moisturizing way to keep shower time fun.',
desc: 'Since we all love being happy naked, why not keep it that way? Treat yourself to scrumptious skincare with Naked Bar, an innovative and moisturizing way to keep shower time fun.'},

I enter the information here and it corresponds to the code here: 我在此处输入信息,它与此处的代码相对应:

      __ += '<li id="wine-list-'+i+'">'+wines[i].name+',</li>';
  full += '<div id="wine-info-'+i+'" class="wine-info transition-05"><h2>'+wines[i].name+'</h2><h4></h4>'+
    '<div class="line"></div>'+
    '<div class="price">$'+wines[i].price+'</div>'+
    '<div class="lbl"><label>Scent:</label><span>'+wines[i].scent+'</span></div>'+
    '<div class="lbl"><label>Size:</label><span>'+wines[i].size+'</span></div>'+
    '<div class="img"></div>'+
    '<div class="bottle-x"></div>'+
    '<div class="desc">'+wines[i].desc+'</div>'+
    '<div class="order">'+wines[i].order+'</div>'+
  '</div>';

Currently, the "order now" button that shows up on that product page appears on each product, but cannot be linked. 当前,该产品页面上显示的“立即订购”按钮出现在每个产品上,但无法链接。 The div class of "order" above was added at the advice of a coder friend; 上面“ order”的div类是在编码朋友的建议下添加的; it makes the button appear, but we can't make it work. 它使按钮出现,但我们无法使其起作用。 I have added this below the "desc" line in the first code block with no luck: 我在第一个代码块的“ desc”行下添加了此代码,但没有运气:

    order: '<a  onClick="location.href='http://google.com'" href="http://google.com"></a>'

So... help! 所以...求助! How do I make this work? 我该如何工作?

The immediate problem of quotes has been addressed by the others. 其他人已经解决了眼前的报价问题。 I will address some of the underlying problems in your code, that will help prevent mistakes in the future. 我将解决您代码中的一些潜在问题,这将有助于防止将来出现错误。

Firstly 首先

var obj = {}

This is a JavaScript Object, specifically an object literal. 这是一个JavaScript对象,特别是对象文字。

Secondly 其次

JavaScript variables cannot start with or only be numbers, so 1 = {}; JavaScript变量不能以数字开头或只能是数字,因此1 = {}; is invalid 是无效的

Instead of assigning your objects to a number and iterating through them, generally what you want is an array of objects. 通常,您需要的是对象数组,而不是将对象分配给数字并遍历它们。 So... 所以...

var wines = [{name:'wine', blah:'', ......}, {name:'second wine', ....}]

This way you can iterate through them in a loop whilst getting wines[i] , and your code will be valid :) 这样,您就可以在获取wines[i]同时循环遍历它们,您的代码将是有效的:)

Thirdly 第三

It is generally a good practice to try to reduce the amount of html that is written to the page by your js. 通常,尝试减少js写入页面的html数量是一个好习惯。

Why? 为什么? It is way easier to debug, as an innerHTML/.html() can contain many statements. 这是一种更容易调试的方法,因为innerHTML / .html()可以包含许多语句。 And it is also faster. 而且速度也更快。

Try to have the elements on the page and write to them using getElementById and textContent , or use jquery's selector and text() or html() . 尝试在页面上放置textContent ,并使用getElementByIdtextContent对其进行写入,或者使用jquery的选择器和text()html() If they need to be hidden, set them as hidden initally in your html/css, and modify that css property or class later with js. 如果需要隐藏它们,请将它们设置为在html / css中初始隐藏,然后在以后使用js修改该css属性或类。

Finally 最后

Inline onClicks are so 90's. 内联onClick大约是90年代。 And will literally cause you headache's when you start double crossing strings. 当您开始使用双弦琴弦时,确实会引起您的头痛。

Use jQuery's on() method or 使用jQuery的on()方法或

document.getElementById('elem').onclick = function(){
   //Code here
};

your quotation marks are out of order. 您的引号不正确。 and why do you need an onClick if you have a href? 以及为什么拥有href却需要onClick? try this: 尝试这个:

order: '<a href="http://google.com">order now</a>'

or this 或这个

order : '<a href="#" onClick="location.href=\"http://google.com\"">order now</a>'

i just had a look at your site, and it seems that the order property is not present. 我只是看了看您的网站,似乎不存在order属性。 here is an example of how exaclty it should look like this. 这是一个这样的例子。 also mind the position of the curly braces, the oder line has to be inside. 还介意花括号的位置, oder行有在里面。

1: {
  name: 'Bar Soap',
  scent: 'Cucumber &amp; Oatmeal, Patchouli, Peppermint, Frankincense, Holiday, Coffee     &amp; Hazelnut, Apple, and more',
  size: '1 bar',
  price: '3.<span class="small">95</span>',
  short_desc: 'An innovative and moisturizing way to keep shower time fun.',
  desc: 'Since we all love being happy naked, why not keep it that way? Treat yourself  to scrumptious skincare with Naked Bar, an innovative and moisturizing way to keep shower time fun.',
  order: '<a href="/order.php">order now</a>'
},

The quotation marks are causing your problems. 引号引起您的问题。

{
  name: 'Bar Soap',
  scent: 'Cucumber &amp; Oatmeal, Patchouli, Peppermint, Frankincense, Holiday, Coffee &amp; Hazelnut, Apple, and more',
  size: '1 bar',
  price: '3.<span class="small">95</span>',
  short_desc: 'An innovative and moisturizing way to keep shower time fun.',
  desc: 'Since we all love being happy naked, why not keep it that way? Treat yourself to scrumptious skincare with Naked Bar, an innovative and moisturizing way to keep shower time fun.',
  order_url: 'http://google.com'
}

Then, do something like this: 然后,执行以下操作:

for (var i in wines) {
      _ += '<li id="wines-'+i+'"><h2>'+wines[i].name+'</h2>'+
        '<div class="lbl"><label>Scent:</label><span>'+wines[i].scent+'</span></div>'+
        '<div class="lbl"><label>Size:</label><span>'+wines[i].size+'</span></div>'+
        '<div class="img"></div>'+
        '<div class="line"></div>'+
        '<div class="short-desc">'+wines[i].short_desc+'</div>'+
        '<div class="price">$'+wines[i].price+'</div>'+
      '</li>';
      __ += '<li id="wine-list-'+i+'">'+wines[i].name+',</li>';
      full += '<div id="wine-info-'+i+'" class="wine-info transition-05"><h2>'+wines[i].name+'</h2><h4></h4>'+
        '<div class="line"></div>'+
        '<div class="price">$'+wines[i].price+'</div>'+
        '<div class="lbl"><label>Scent:</label><span>'+wines[i].scent+'</span></div>'+
        '<div class="lbl"><label>Size:</label><span>'+wines[i].size+'</span></div>'+
        '<div class="img"></div>'+
        '<div class="bottle-x"></div>'+
        '<div class="desc">'+wines[i].desc+'</div>'+
        '<a href="'+wines[i].order_url+'"><div class="order"></div></a>'+
      '</div>';
    }

Also, don't forget to define the "order" attribute for all of your items. 另外,不要忘记为所有商品定义“订单”属性。

Hope that helps. 希望能有所帮助。

暂无
暂无

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

相关问题 创建一个包含JavaScript的链接 - Create a link with javascript within it 如何创建对javascript文件中链接的引用? - How would I create a reference to a link within a javascript file? jQuery Accordion 的 JavaScript 关闭问题(我相信) - JavaScript closure issue (I believe) with jQuery Accordion 我试图将CORS正确地实现到我的JavaScript代码,并且工作了一段时间,我相信项目中缺少一些文件吗? - I am trying to implement CORS to my JavaScript code correctly, and it worked for a while, I believe I am missing some file from the project? 尝试在对象Javascript中创建嵌套数组 - Trying to create nested array within an Object Javascript 我正在尝试创建一个圆形进度条,但我无法访问 javascript 变量,为什么? - I am trying to create a circle progress bar, but I can not access the javascript variables, why? 我正在尝试使用用户通过JavaScript警报框输入的信息在HTML页面上创建链接 - I am trying to create a link on an HTML page using information that a user inputs via JavaScript alert box 如何在使用javascript的图片中使用指定的链接组创建随机链接? - How do I create a random link out of specified group of links using within an image using javascript? 将javascript变量插入交易电子邮件中的href链接 - Insert javascript variables into a href link within a transactional email 我正在尝试使链接弹出JavaScript - I am trying to make a link popup a javascript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM