【问题标题】:Direction rtl to all page方向 rtl 到所有页面
【发布时间】:2016-11-15 17:53:50
【问题描述】:

我的 html 页面很长。
我想将所有文本/表格的方向更改为 rtl。
我怎样才能对所有页面执行此操作,而不是更改为指向 rtl 的方向的任何元素?
谢谢。

【问题讨论】:

  • 我如何用 javascript 做到这一点?我可以为 body 元素添加属性吗?
  • 是的。 document.body.setAttribute('dir', 'rtl')

标签: javascript html css


【解决方案1】:

使用 CSS:

* {
    direction: rtl;
}

使用 JavaScript:

var children = document.children;
var i;
for (i = 0; i < children.length; i++) {
   children[i].style.direction = "rtl";
}

document.body.style.direction = "rtl";

或(来自evolutionxbox 评论)

document.body.setAttribute('dir', 'rtl')

使用 JQuery(即使你不是在询问)

$("html").children().css("direction","rtl");

内联编码:

<body dir="rtl">

<html dir="rtl">

【讨论】:

  • document.body.setAttribute('dir', 'rtl')
  • 谢谢,document.children 可能是未定义的?
  • @CSharpBeginner No. document.children 至少返回一项(body - 因为如果它不存在于您的代码中,它会由 Web 浏览器事件自动添加)。
【解决方案2】:

你可以使用 jQuery。

$('body').children().css('direciton','rtl');

或者使用纯Javascript:

document.getElementsByTagName('Body')[0].style.direction = "rtl"

【讨论】:

  • amm..我的 .html 页面中可能没有 标签?
  • 我不这么认为。无论如何,您可以使用任何元素(如 Div 容器)并使用相同的代码....
  • 如果 OP 没有用它标记问题,请不要给出 jQuery 答案。
  • @CSharpBeginner - 如果 HTML 中不存在 body 标签,浏览器将自动添加它。
  • 你可以用纯Javascrip执行同样的操作:document.getElementsByTagName('Body')[0].style.direction = "rtl"
【解决方案3】:

Javascript

 document.body.style.direction= "rtl";

HTML

<body dir="rtl">

【讨论】:

  • document.body.setAttribute('dir', 'rtl')
  • 是的,我们可以这样做
【解决方案4】:

CSS 中的 direction 属性设置块级元素内内容流的方向。这适用于文本、内联和内联块元素。它还设置文本的默认对齐方式和表格单元格在表格行中流动的方向。您可以使用

body {
  direction: rtl;  /* Right to Left */
}

或者还有一个HTML属性可以设置方向

<body dir="rtl">

建议您使用 HTML 属性,因为即使 CSS 失败或因任何原因不影响页面,该属性也可以工作。

【讨论】:

    【解决方案5】:

    使用 css

    html * {
      direction: rtl;
    }
    

    使用 jQuery

    $('html').children().css('direction','rtl');
    

    【讨论】:

    • 如果 OP 没有用它标记问题,请不要给出 jQuery 答案。
    • @evolutionxbox 但我首先添加了 css 解决方案.. jquery 是附加的
    • 没关系,但请包含非 jquery JS 解决方案。因为这就是 OP 所要求的。 “我如何使用 javascript 来做到这一点?我可以为 body 元素添加属性?”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-28
    • 1970-01-01
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多