javascript - 在 Javascript 中更改动态链接文本

标签 javascript jquery html css

我目前有一个使用 HTMl CSS 和 JS 运行的幻灯片,幻灯片下方的导航按钮只是放置数字而不是文本。有没有办法获取图像标题并使用它或为每个幻灯片链接使用自定义文本。下面我包含了制作导航按钮和添加文本的 Javascript。如果您还需要什么,请告诉我。

如果我可以在这个 JS 文件中指定文本也可以。

此外,如果它可以帮助我使用 Kickstart HTML 模板。 查看链接http://bliskdesigns.com/clients/timbr/

var items = $(this).find('li');
    wrap.append('<ul class="slideshow-buttons"></ul>');
    items.each(function(index){
        wrap.find('.slideshow-buttons')
        .append('<li><a href="#slideshow-'+index+'" rel="'+index+'">'+(index+2)+'</a></li>');
    });

最佳答案

但是,很难根据您提供的内容给您一个简明的答案:

假设在您提供的代码中:

  • items 是包含幻灯片中每张幻灯片的数组;
  • items 中的每个元素都包含一个 img
  • 您希望在幻灯片导航中显示的文本是每个 imgtitle 属性;
  • wrap 构建的无序列表是导航;
  • 您要更改的文本是注入(inject)到 wrap 中的每个元素的 anchor 中的数字。

这是一个可能的答案:

// put all slides into 'items'
var items = $(this).find('li');

// append the wrap element with an unordered list
wrap.append('<ul class="slideshow-buttons"></ul>');

// loop over each item
items.each(function(index){
    // grab the title attribute of the img child of this instance of items
    var titleText = $(this).find('img').attr('title');  

    // push a new <li> into 'wrap'
    wrap.find('.slideshow-buttons').append('<li><a href="#slideshow-'+index+'" rel="'+index+'">'+titleText+'</a></li>');
});

这应该只是您元素中您上面包含的代码的来源的直接替换。

正如我所说:我不能保证在没有更多信息的情况下这会起作用,但理论上它会起作用。确保你的每张图片都有一个标题:

<img src="/link/to/image.kpg" alt="alternative text" title="text you want to appear in the slider navigation" >

或者,您可以使用图像的 alt 标记中的文本,而不是通过更改上面的这一行:

// grab the alt attribute of the img child of this instance of items
var titleText = $(this).find('img').attr('alt');

关于javascript - 在 Javascript 中更改动态链接文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20288440/

相关文章:

javascript - 如何使用 Ember 将动态段的值输出到页面上?

javascript - 用于手机的条形码扫描器,用于表单中的网站

javascript - ES6 箭头函数和 setTimeOut

javascript - 如何在 ajax 上使用 this.children

html - 将 div 元素固定到页面缩放时的位置,而不是滚动时

javascript - 单击嵌套的 Bootstrap Dropdown <li> 时防止滚动

javascript - 我应该在 vb.net 上做什么来显示我的模式弹出窗口

c# - MVC4从SQL Server表检索图像文件到html图像

javascript - 在每次上传文件时使用 dropzone.js 发送自定义数据

jquery - 页面导航(页面内)基于没有浏览器历史记录的 anchor 标记(或附加到 url 的#anchor)